为什么子图比图快得多? [英] Why is subplot much faster than figure?
问题描述
我正在MATLAB中建立数据分析平台.该系统的功能之一需要创建许多图.在任何给定时间,只有一个图可用,并且用户可以根据要求遍历下一个/上一个图(这里的重点是不需要打开多个窗口).
最初,每次显示新图时,我都使用figure
命令,但是我注意到,当用户遍历下一个图时,该命令花费的时间比我想要的要长一些.降低可用性.因此,我尝试使用subplot
代替它,它的工作速度更快.
看到这种行为,我进行了一些实验,同时进行了计时. figure
第一次运行大约需要0.3秒,而subplot
大约需要0.1秒. figure
的平均运行时间为0.06秒,标准偏差为0.05,而subplot
仅花费0.002,标准偏差为0.001.似乎subplot
快了一个数量级.
问题是:在任何给定时间只有一个窗口可用的情况下,有没有使用数字的理由?
使用'subplot'通常会失去任何价值吗?
(即使您只能一次,也可以进行类似的考虑).
axes
对象以及一些方便的定位选项.
轴对象始终是图对象的子对象,因此,如果没有打开figure
窗口,则subplot
将打开一个窗口.此操作需要一些时间.因此,与其为每个新图打开一个新图形窗口,不如按照您的正确判断,使用subplot
创建一个新的轴对象会更快.要节省一些内存,您可以通过 clf
作为 由丹尼尔建议 .
据我了解,您不想在平铺位置创建轴,而只想创建一个轴对象.因此,使用 axes
甚至会更快. > 命令. subplot
实际上是多余的.
如果所有绘图都具有相同的轴限制和标签,甚至> clf
是不必要的.使用 cla
(清晰的轴) )删除上一个图,但保留标签,界限和网格.
示例:
%// plot #1
plot( x1, y2 );
xlim( [0,100] ); ylim( [0,100] );
xlabel( 'x' );
ylabel( 'y' );
%// clear plot #1, keep all settings of axes
%// plot #2
plot( x2, y2 );
...
I'm building a data analysis platform in MATLAB. One of the system's features need to create many plots. At any given time only one plot is available and the user can traverse to the next/previous upon request (the emphasis here is that there is no need for multiple windows to be open).
Initially I used the figure
command each time a new plot was shown, but I noticed that, as the user traverse to the next plot, this command took a bit longer than I wanted. Degrading usability. So I tried using subplot
instead and it worked much faster.
Seeing this behavior I ran a little experiment, timing both. The first time figure
runs it takes about 0.3 seconds and subplot
takes 0.1 seconds. The mean run time for figure
is 0.06 seconds with standard deviation of 0.05, while subplot
take only 0.002 with standard deviation of 0.001. It seems that subplot
is an order of magnitude faster.
The question is: In situation when only one window will be available at any given time, is there any reason to use figure?
Is there any value lost in using `subplot' in general?
(similar consideration can be made even if you can either only once).
The call of subplot
does nothing else than creating a new axes
object with some convenient positioning options wrapped around.
Axes objects are always children of figure objects, so if there is no figure
window open, subplot
will open one. This action takes a little time. So instead of opening a new figure window for every new plot, it's faster to just create a new axes object by using subplot
, as you determined correctly. To save some memory you can clear the previous plot by clf
as suggested by Daniel.
As I understood, you don't want to Create axes in tiled positions, rather you just want to create one axes object. So it would be even faster to use the axes
command directly. subplot
is actually overkill.
If all your plots have the same axes limits and labels, even clf
is not necessary. Use cla
(clear axes) to delete the previous plot, but keep labels, limits and grid.
Example:
%// plot #1
plot( x1, y2 );
xlim( [0,100] ); ylim( [0,100] );
xlabel( 'x' );
ylabel( 'y' );
%// clear plot #1, keep all settings of axes
%// plot #2
plot( x2, y2 );
...
这篇关于为什么子图比图快得多?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!