在不将人物置于前景的情况下绘制人物
Plot to figures without bringing them into foreground
figure;
ax1 = axes;
figure;
ax2 = axes;
x = 0; y = 0;
while ishandle(ax1) && ishandle(ax2)
x = x + 1;
y = y + 1;
figure(1)
scatter(x,y, 'MarkerEdgeColor', 'red')
hold on
figure(2)
scatter(x,y, 'MarkerEdgeColor', 'blue')
hold on
end
在我的脚本中我有多个图形,它们将循环更新。数字必须显示,而脚本是运行。不幸的是,当前更新的人物总是在前台弹出,这使得无法监视某个人物。我知道 figure(1)
和 figure(2)
的调用会导致这种行为,但我如何绘制这些数字,而不将 window 置于前台?
正如 mikkola 在评论中所建议的,您可以指定向 scatter
或 plot
轴添加数据点。然而,有一个更好的方法:创建一个单行对象,并更新它的 xdata
和 ydata
属性。这既更快又更节省内存。您的代码将变为:
x = 0; y = 0;
figure;
h1 = plot(x,y,'ro');
figure;
h2 = plot(x,y,'bo');
while ishandle(h1) && ishandle(h2)
x = x + 1;
y = y + 1;
h1.XData(end+1) = x;
h1.YData(end+1) = y;
h2.XData(end+1) = x;
h2.YData(end+1) = y;
drawnow
pause(0.1)
end
在使用 MATLAB 处理图形时,我有一套经验法则。这些与这个问题相关:
仅使用 figure
来创建新图形,或将现有图形置于最前面(您通常希望避免这种情况,但有时是必要的)。
始终通过保留和使用它们的句柄来指定要使用的图或轴。我从不依赖 gcf
或 gca
(既不明确也不隐含)。在命令行上输入时使用当前图形很有用,但在脚本或函数中,真正的危险是有人在函数执行时随机单击 windows。创建一个 window 然后写入 gcf
可能最终会写入一个不同的数字(真的,我总是点击随机的东西)。
不要创建不必要的对象。为绘制的每个点创建一个新的 line
对象是一种浪费。
另请注意,除非您为每个点指定不同的颜色或大小,否则 plot(...'o')
等同于 scatter(...)
。但是使用点大小或颜色来指定附加信息并不是传达该信息的好方法。如果您有兴趣了解通过图表进行有效沟通,请阅读 Tufte's "The visual display of quantitative information"。
相关部分可以在包含输入 ax
:
的 documentation of scatter
部分找到
scatter(ax,___)
plots into the axes specified by ax
instead of into
the current axes.
这允许用户指定一个轴句柄,指向哪些轴应该用于绘制散点图。因此,如果您跳过在代码中使用 figure
并改用 ax
输入,则可以避免与 figure
.
关联的 "bring to front" 行为
您可以修改您的代码如下:
figure;
ax1 = axes;
figure;
ax2 = axes;
x = 0; y = 0;
while ishandle(ax1) && ishandle(ax2)
x = x + 1;
y = y + 1;
scatter(ax1, x,y, 'MarkerEdgeColor', 'red')
hold on
scatter(ax2, x,y, 'MarkerEdgeColor', 'blue')
hold on
end
figure;
ax1 = axes;
figure;
ax2 = axes;
x = 0; y = 0;
while ishandle(ax1) && ishandle(ax2)
x = x + 1;
y = y + 1;
figure(1)
scatter(x,y, 'MarkerEdgeColor', 'red')
hold on
figure(2)
scatter(x,y, 'MarkerEdgeColor', 'blue')
hold on
end
在我的脚本中我有多个图形,它们将循环更新。数字必须显示,而脚本是运行。不幸的是,当前更新的人物总是在前台弹出,这使得无法监视某个人物。我知道 figure(1)
和 figure(2)
的调用会导致这种行为,但我如何绘制这些数字,而不将 window 置于前台?
正如 mikkola 在评论中所建议的,您可以指定向 scatter
或 plot
轴添加数据点。然而,有一个更好的方法:创建一个单行对象,并更新它的 xdata
和 ydata
属性。这既更快又更节省内存。您的代码将变为:
x = 0; y = 0;
figure;
h1 = plot(x,y,'ro');
figure;
h2 = plot(x,y,'bo');
while ishandle(h1) && ishandle(h2)
x = x + 1;
y = y + 1;
h1.XData(end+1) = x;
h1.YData(end+1) = y;
h2.XData(end+1) = x;
h2.YData(end+1) = y;
drawnow
pause(0.1)
end
在使用 MATLAB 处理图形时,我有一套经验法则。这些与这个问题相关:
仅使用
figure
来创建新图形,或将现有图形置于最前面(您通常希望避免这种情况,但有时是必要的)。始终通过保留和使用它们的句柄来指定要使用的图或轴。我从不依赖
gcf
或gca
(既不明确也不隐含)。在命令行上输入时使用当前图形很有用,但在脚本或函数中,真正的危险是有人在函数执行时随机单击 windows。创建一个 window 然后写入gcf
可能最终会写入一个不同的数字(真的,我总是点击随机的东西)。不要创建不必要的对象。为绘制的每个点创建一个新的
line
对象是一种浪费。
另请注意,除非您为每个点指定不同的颜色或大小,否则 plot(...'o')
等同于 scatter(...)
。但是使用点大小或颜色来指定附加信息并不是传达该信息的好方法。如果您有兴趣了解通过图表进行有效沟通,请阅读 Tufte's "The visual display of quantitative information"。
相关部分可以在包含输入 ax
:
scatter
部分找到
scatter(ax,___)
plots into the axes specified byax
instead of into the current axes.
这允许用户指定一个轴句柄,指向哪些轴应该用于绘制散点图。因此,如果您跳过在代码中使用 figure
并改用 ax
输入,则可以避免与 figure
.
您可以修改您的代码如下:
figure;
ax1 = axes;
figure;
ax2 = axes;
x = 0; y = 0;
while ishandle(ax1) && ishandle(ax2)
x = x + 1;
y = y + 1;
scatter(ax1, x,y, 'MarkerEdgeColor', 'red')
hold on
scatter(ax2, x,y, 'MarkerEdgeColor', 'blue')
hold on
end