MATLAB:保存散点图需要太长时间

Posted

技术标签:

【中文标题】MATLAB:保存散点图需要太长时间【英文标题】:MATLAB: saving scatter plot takes too long 【发布时间】:2016-08-04 15:06:12 【问题描述】:

我正在使用 scatter 函数绘制仅包含 25000 行的数据,但是当我使用 saveas 函数将图像保存为 png 时,需要 2 分钟以上的时间。

例如:

scatter(x,y,'filled');
pic_name = ['scatterplot.png'];
tic
saveas(gcf,pic_name,'png');
toc

Elapsed time is 152.681511 seconds.

有没有更快的方法来保存散点图?

【问题讨论】:

在我的电脑中花费 3 秒。我有一个很好的,但仍然有很大的不同。 25000 值也不是“唯一的”,它的值相当高。 没错,但我仍然可以很快保存具有 500k 行数据的常规图。 @BehnazEnsan 你试过使用export_fig吗? mathworks.com/matlabcentral/fileexchange/23629-export-fig ~2.5s 在我的(好)电脑上。我尝试了 Matlab R2009a、R2013a 和 R2016a 来查看版本是否会有所不同 => 所有版本的速度相同。但是,在我第二次运行它之后,我下降到 0.5 秒。如果您使用plot 而不是scatter,这是否需要相同的时间? @Hoki 当我使用plot时,将其保存为png需要0.3秒。 :(顺便说一句,我使用的是 R2012b。 【参考方案1】:

scatter 对于大量数据点来说速度很慢;这可能包括保存它们(每个点都有不同的颜色,因此需要单独处理)。

您可能想尝试以不同方式绘制数据,如下所述:matlab: scatter plots with high number of datapoints 这样,您将获得与常规绘图相同的行为。

【讨论】:

【参考方案2】:

您可以尝试调用 getframe(),然后调用 imwrite(),而不是调用 saveas(),如下所示:

npoints = 25000;
x = linspace(0,3*pi, npoints);
y = cos(x) + rand(1, npoints);

scatter_series_handle = scatter(x,y,'filled');

pic_name = ['scatterplot.png'];

axes_handle   = get(scatter_series_handle, 'Parent');
figure_handle = get(axes_handle, 'Parent');

img = getframe(figure_handle);
imwrite(img.cdata, pic_name, 'png');    

【讨论】:

以上是关于MATLAB:保存散点图需要太长时间的主要内容,如果未能解决你的问题,请参考以下文章

matlab已知散点图如何拟合

如何用matlab 画散点图 如何标记数据点的颜色

如何根据散点图用MATLAB拟合函数图象?

还在用Excel画散点图?不试试极坐标散点图?

matlab中画散点图时怎样在每个点上标记符号?

用MATLAB的函数scatter()绘制散点图