matlab: how do I do animated plot in a figure of two subplot
有一个网络示例,展示了如何在单个图形中进行动画绘图。
但是,我想在一个图形中做两个子图,这样它们将在第一个子图中显示动画,然后在第二个子图中显示动画。
使用
所以我正在寻找的效果是:1)已打开并具有两个子图的图形。 2)在第一个子图中绘制动画曲线,然后在第二个子图中绘制动画曲线。 3)我想回到第一个子图以绘制更多的东西,并且也要回到第二个子图以绘制更多的东西。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | figure(1); hold on; x = 1:1000; y = x.^2; %// Plot starts here figure,hold on %// Set x and y limits of the plot xlim([min(x(:)) max(x(:))]) ylim([min(y(:)) max(y(:))]) %// Plot point by point for k = 1:numel(x) plot(x(k),y(k),'-') %// Choose your own marker here %// MATLAB pauses for 0.001 sec before moving on to execue the next %%// instruction and thus creating animation effect pause(0.001); end |
只需在循环中执行
1 2 3 4 5 6 7 8 9 10 11 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | % Easiest way x = rand(1, 11); y = rand(1, 11); z = rand(1, 11); a = rand(1, 11); figure for i = 1 : 10 subplot(211) plot(x(i : i+1), y(i : i+1), '.-k'); hold on; % include this if you want to show plot history subplot(212) plot(z(i : i+1), a(i : i+1), '.-k'); drawnow; pause(0.1); end % If you don't want to call"plot" interatively x = rand(1, 11); y = rand(1, 11); z = rand(1, 11); a = rand(1, 11); figure subplot(211) p1 = plot(NaN, NaN, 'marker', 'o'); subplot(212) p2 = plot(NaN, NaN, 'marker', 'd'); for i = 1 : 10 set(p1, 'xdata', x(i : i+1), 'ydata', y(i : i+1)); set(p2, 'xdata', z(i : i+1), 'ydata', a(i : i+1)); drawnow; pause(0.1); end |
首先将情节定义为构造,因此
1 |
这将更新图