How to keep axes box fixed when creating an animation

32 次查看(过去 30 天)
Hi,
I have data from ode45 that I want to plot in space at every time step. To do this, I create a for loop over the time output from ode45 and plot the position at every point. Here is my code:
for i=1:length(t)
plot3(x(:,1,i),x(:,2,i),x(:,3,i),'k*')
hold on
plot3(x(:,1,i)+d(:,1,1,i),x(:,2,i)+d(:,2,1,i),x(:,3,i)+d(:,3,1,i),'k*')
grid on
axis equal
xlabel('x')
ylabel('y')
zlabel('z')
camtarget([0 0 0])
campos([2 2 L/2])
camup([1 0 0])
axis([-1.2,1.2,-1.2,1.2])
hold off
drawnow
end
This works pretty well, but, despite my definition for the axis limits, the plot box deforms as I iterate through time. How can I keep it rigid?

回答(1 个)

Ameer Hamza
Ameer Hamza 2018-5-2
Every time you call plot3(), you are actually creating a new axes object. The new axis object will overwrite the properties of previous axes hence axis([-1.2,1.2,-1.2,1.2]) while no longer be effective. The proper way is to use line handle to update the animation. Use following code as template
l1 = plot3(0,0,0);
l1.XData = [];
l1.YData = [];
l1.ZData = [];
l1.LineStyle = 'none';
l1.MarkerEdgeColor = 'r';
l1.Marker = '*';
hold on
l2 = plot3(0,0,0);
l2.XData = [];
l2.YData = [];
l2.ZData = [];
l2.LineStyle = 'none';
l2.MarkerEdgeColor = 'g';
l2.Marker = '*';
axis([0 1 0 1 0 1]);
for i=1:100
l1.XData = rand(1, 100);
l1.YData = rand(1, 100);
l1.ZData = rand(1, 100);
l2.XData = rand(1, 100);
l2.YData = rand(1, 100);
l2.ZData = rand(1, 100);
pause(1);
end
  2 个评论
Roy Goodman
Roy Goodman 2021-4-12
This makes sense to me for plotting a single line object, or even if a fixed number of line objects.
I am writing a function that creates an animation from a numerical data set which may have an arbitrary number of line objects at each step. In my case the number of lines is fixed from frame to frame within an animation, but will vary between two animations.
Roy Goodman
Roy Goodman 2021-4-12
Nevermind. The obvious answer, cell arrays, works great. Just replace l1 and l2 in the above code with l{1} and l{2}.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Animation 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by