How to keep axes box fixed when creating an animation

조회 수: 60(최근 30일)
Evan Hemingway
Evan Hemingway 2018년 3월 13일
댓글: Roy Goodman 2021년 4월 12일
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일
Nevermind. The obvious answer, cell arrays, works great. Just replace l1 and l2 in the above code with l{1} and l{2}.

댓글을 달려면 로그인하십시오.

범주

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!

Translated by