关于animation:matlab:如何在两个子图中创建动画图

matlab: how do I do animated plot in a figure of two subplot

有一个网络示例,展示了如何在单个图形中进行动画绘图。

但是,我想在一个图形中做两个子图,这样它们将在第一个子图中显示动画,然后在第二个子图中显示动画。

使用'figure(1)''figure (2)''hold on',我可以进行如下动画绘制。但是,如何调用子图来做类似的事情?

所以我正在寻找的效果是: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

只需在循环中执行subplot

1
2
3
4
5
6
7
8
9
10
11
for k = 1:numel(x)
    subplot(1,2,1)
    plot(x(k),y(k),'-') %// Choose your own marker here

    subplot(1,2,2)
    plot(x(1:k),y(1:k))

    %// 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
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

首先将情节定义为构造,因此p1 = plot(x,y)。然后,您设置您的循环,并在循环中您的write

1
  set(p1,'YData',y);

这将更新图p1的YData,即y。如果要以动画形式查看它,只需在set之后添加pause(0.1) %seconds