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

问题描述 投票:17回答:2

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

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

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

matlab plot
2个回答
18
投票

你可以使用类似的东西

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

figure(2)
plot(z,w) % this will go on another figure

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

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

h=figure(...)

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

set(0,'CurrentFigure',h)

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')

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

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

figure
plot(2,2)

plot(hAx1, 3, 3)

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

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

© www.soinside.com 2019 - 2024. All rights reserved.