【问题标题】:matlab: how do I do animated plot in a figure of two subplotmatlab:如何在两个子图的图中制作动画图
【发布时间】:2014-11-26 01:20:27
【问题描述】:

有一个网络示例显示了如何在单个图形中制作动画情节。

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

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

所以我正在寻找的效果是:1)打开并有两个子图的图。 2)在第一个子图中绘制动画曲线,然后在第二个子图中绘制动画曲线。 3)我想回到第一个子图来绘制更多的东西,也想去第二个子图来绘制更多的东西。

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

【问题讨论】:

    标签: matlab animation plot


    【解决方案1】:

    只需在循环中执行subplot

    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
    

    【讨论】:

    • 我是否也要在 subplot 121 之后添加 hold on ?
    • 取决于你想做什么,但你可以。
    • 因为我打算逐点更新每个子图(比如子图 1,我绘制 1 个点……然后是子图 2,我绘制另一个点,然后重复),所以我只是在循环内坚持?
    【解决方案2】:
    % 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
    

    【讨论】:

      【解决方案3】:

      首先将你的情节定义为一个构造,所以p1 = plot(x,y)。然后你设置你的循环并在循环中你的写

        set(p1,'YData',y);
      

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

      【讨论】:

      • 虽然我更喜欢这种解决方案(设置XData/YData)而不是每次迭代都必须调用plotsubplot,但您应该更详细地说明这些步骤......并且还要注意您使用N 数据点xy 创建了第一个图...当您尝试仅使用包含比XData 更多点的向量更新YData 时,Matlab 会抱怨。你必须同时更新!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-04
      • 1970-01-01
      • 2018-04-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多