绘制数字而不使其成为前景 [英] 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)
会导致这种行为,但是我如何绘制这些图而又不将窗口置于前台?
In my script I have multiple figures, which are going to be updated in a loop. The figures have to be displayed, while the script is running. Unfortunately the currently updated figure is always popping in the foreground, which makes it impossible to monitor a certain figure. I understand that the calling of figure(1)
and figure(2)
causes this behaviour, but I how can I plot to these figures, without bringing the window into foreground?
推荐答案
正如mikkola在注释中建议的那样,您可以指定scatter
或plot
的轴添加数据点.但是,有一个更好的方法:创建单个线对象,并更新其xdata
和ydata
属性.这既更快又更有效.您的代码将变为:
As mikkola suggested in a comment, you can specify to which axes scatter
or plot
add data points. However, there is a better method: create a single line object, and update its xdata
and ydata
properties. This is both faster and more memory efficient. Your code would become:
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处理图形时,我保留了一组经验法则.这些与该问题有关:
I keep a set of rules of thumb for when working with MATLAB handle graphics. These are relevant to this question:
-
仅使用
figure
创建一个新图形,或将一个现有图形放在最前面(通常要避免,但有时是必须的).
Use
figure
only to create a new figure, or to bring an existing figure to the front (which you want to avoid in general, but sometimes is necessary).
始终通过保持和使用其手柄来指定要使用的图形或轴.我从不依赖gcf
或gca
(既不明确也不隐含).在命令行上键入命令时,使用当前图形很有用,但是在脚本或函数中,真正的危险在于,在函数执行过程中,有人随机单击窗口.创建一个窗口然后写入gcf
可能最终会写入另一个图形(实际上,我一直都在单击随机的东西).
Always specify with which figure or axes you want to work, by keeping and using their handles. I never rely on gcf
or gca
(not explicitly nor implicitly). Using the current figure is useful when typing on the command line, but in a script or a function there is the real danger than someone clicks randomly on windows while the function is executing. Creating a window then writing to gcf
could end up writing to a different figure (really, I click on random things all the time).
不要创建过多的对象.为您绘制的每个点创建一个新的line
对象很浪费.
Don't create more objects than necessary. Creating a new line
object for every point you plot is wasteful.
还请注意,除非为每个点指定不同的颜色或大小,否则plot(...'o')
等效于scatter(...)
.但是,使用点大小或颜色指定其他信息不是传达该信息的好方法.如果您有兴趣学习有效的交流,请阅读 Tufte的定量信息的可视化显示" 通过图形.
Note also that plot(...'o')
is equivalent to scatter(...)
unless you specify a different color or size for each point. But using the dot size or color to specify additional information is not a good way to convey that information. Read Tufte's "The visual display of quantitative information" if you're interested in learning about effective communication through graphs.
这篇关于绘制数字而不使其成为前景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!