如何指定绘图应该是哪个数字?

Ist*_*har 17 matlab plot

我有多个数字打开,我想在运行时独立更新它们.以下玩具示例应阐明我的意图:

clf;

figure('name', 'a and b'); % a and b should be plotted to this window
hold on;
ylim([-100, 100]);

figure('name', 'c'); % only c should be plotted to this window

a = 0;
b = [];
for i = 1:100
    a = a + 1;
    b = [b, -i];
    c = b;
    xlim([0, i]);
    plot(i, a, 'o');
    plot(i, b(i), '.r');
    drawnow;
end
Run Code Online (Sandbox Code Playgroud)

这里的问题是,当我打开第二个时figure,我无法告诉plot函数绘制到第一个而不是第二个(并且只c应绘制到第二个).

min*_*ive 18

你可以使用类似的东西

figure(1)
plot(x,y) % this will go on figure 1

figure(2)
plot(z,w) % this will go on another figure
Run Code Online (Sandbox Code Playgroud)

该命令还将图形设置为可见并位于所有内容之上.

您可以根据需要通过发出相同的figure命令在数字之间来回切换.或者,您也可以使用图中的句柄:

h=figure(...)
Run Code Online (Sandbox Code Playgroud)

然后发出figure(h)而不是使用数字索引.使用此语法,您还可以通过使用防止图形弹出顶部

set(0,'CurrentFigure',h)
Run Code Online (Sandbox Code Playgroud)


tim*_*tim 15

您可以在plot-command中指定axes-object.看这里:

http://www.mathworks.de/help/techdoc/ref/plot.html

因此,打开一个图形,插入轴,保存轴对象的id,然后绘制到其中:

figure
hAx1 = axes;
plot(hAx1, 1, 1, '*r')
hold on

figure
hAx2 = axes;
plot(hAx2, 2, 1, '*r')
hold on


plot(hAx2, 3, 4, '*b')
plot(hAx1, 3, 3, '*b')
Run Code Online (Sandbox Code Playgroud)

或者,您可以使用gca而不是自己创建轴对象(因为它在实际图形中自动创建,当它不存在时!)

figure
plot(1,1)
hAx1 = gca;
hold on

figure
plot(2,2)

plot(hAx1, 3, 3)
Run Code Online (Sandbox Code Playgroud)

请参阅以下层次结构,表示图形和轴之间的关系

在此输入图像描述

来自http://www.mathworks.de/help/techdoc/learn_matlab/f3-15974.html.

  • 每个图形通常有多个轴.见`subplot`. (5认同)
  • 因为你绘制的东西总是进入一个轴对象(你不能在没有轴的情况下绘图);)当你不使用`axes`-command时,`plot`会在图形不包含时自动创建它们.所以这是正确的方法.看我编辑的帖子! (3认同)
  • 谢谢,但我仍然想知道,为什么`plot`命令使用轴手柄而不是图形手柄 - 它看起来更直观. (2认同)