creating an animated video by rotating a surface plot
37 views (last 30 days)
Show older comments
I'm struggling to get a consistent animation that has fixed axis when rotating my surface plot. Two of the problems i have faced are:
1) the data size of my surfplot changes as the rotation angle changes. 2) the color of the surface plot is not retained when i store the data in a structure. 3) So far I have compiled this:
number_of_frames = 360;
n=1;
for i = 1:10:number_of_frames
hSurface = surf(velocityimg);
grid off
rotate(hSurface,[0 0 1],i);
newdata(n).x = hSurface.XData;
newdata(n).y = hSurface.YData;
newdata(n).z = hSurface.ZData;
surf(newdata(n).x,newdata(n).y,newdata(n).z);
grid off
hAxes = gca;
hAxes.XRuler.Axle.LineStyle = 'none';
axis off
axis vis3d
colormap(jet)
g(n) = getframe(gcf);
g(n) = frame2im(g(n));
n=n+1;
end
%%make video
v = VideoWriter('rotating.avi');
v.FrameRate = 10;
v.Quality = 65;
open(v);
for i =1:length(g)
frame = g(i).cdata;
writeVideo(v,frame);
end
close(v);
winopen('rotating.avi');
1 Comment
Sky Nelson-Isaacs
on 6 Nov 2019
I have the same issue. How do I keep the axes consistent as I rotate the frame and take snapshots?
Answers (1)
Jaswanth
on 15 Oct 2024
Hi,
To create an animated video by rotating a surface plot in MATLAB while ensuring the axis remains consistent and the color is retained throughout the animation, you can keep the axis limits stable using “axis tight” command, and “axis vis3d” ensures the aspect ratio remains fixed during the 3D rotation.
As you rotate the surface plot, adjust the view angle incrementally, capturing each frame with “getframe”. These frames are then compiled into a video using “writeVideo”.
Please refer to the following example code with assumed data:
% Create sample data for the surface plot
[X, Y] = meshgrid(-5:0.5:5, -5:0.5:5);
Z = sin(sqrt(X.^2 + Y.^2));
% Set up the figure and surface plot
figure;
hSurface = surf(X, Y, Z);
shading interp; % Enable smooth color transitions
colormap jet; % Ensure a consistent color scheme
axis tight; % Keep axis limits stable
axis vis3d; % Fix the aspect ratio for 3D rotation
% Set up the video writer
v = VideoWriter('rotating_surface_plot.avi');
open(v);
% Specify the number of frames for the animation
numFrames = 360;
% Rotate the surface plot and capture frames
for k = 1:numFrames
% Change the view angle
view([k, 30]);
% Capture the current frame
frame = getframe(gcf);
writeVideo(v, frame);
end
% Complete the video writing process
close(v);
% Close the figure display
close(gcf);
Kindly refer to the following MathWorks documentation to know more about the functions discussed above:
- axis tight: https://www.mathworks.com/help/matlab/ref/axis.html?searchHighlight=axis%20tight&s_tid=srchtitle_support_results_1_axis%20tight#bul_eox-1
- axis vis3d: https://www.mathworks.com/help/matlab/ref/axis.html?searchHighlight=axis%20vis3d&s_tid=srchtitle_support_results_1_axis%20vis3d
- getframe: https://www.mathworks.com/help/matlab/ref/getframe.html
I hope the solution provided above is helpful.
0 Comments
See Also
Categories
Find more on Animation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!