How to keep axes box fixed when creating an animation
조회 수: 50 (최근 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?
댓글 수: 0
답변 (1개)
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
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
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 Center 및 File Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!