0

I'm using scatter function to plot data containing only 25000 rows, however when I use saveas function to save the image as png, it takes 2+ minutes to do so.

For example:

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

Elapsed time is 152.681511 seconds.

Is there a quicker way to save a scatter plot?

Ander Biguri
  • 35,140
  • 11
  • 74
  • 120
Bensa
  • 127
  • 2
  • 13
  • 1
    Takes 3s in my PC. I have a good one, but still, the difference is massive. Also 25000 values its not "only", its a quite high amount of values. – Ander Biguri Aug 04 '16 at 15:09
  • True but I could still save regular plots having 500k rows of data pretty quickly. – Bensa Aug 04 '16 at 15:16
  • @BehnazEnsan Have you tried using `export_fig`? http://www.mathworks.com/matlabcentral/fileexchange/23629-export-fig – rayryeng Aug 04 '16 at 16:14
  • ~2.5s on my (good) pc. I tried Matlab R2009a, R2013a and R2016a to see if the version would make a difference => Same speed for all versions. However, after second time I run it, I am down to 0.5s. Does this take the same time for you if you use `plot` instead of `scatter` ? – Hoki Aug 04 '16 at 16:25
  • @Hoki When I use `plot`, it takes 0.3 seconds to save it as png. :( Btw, I'm using R2012b. – Bensa Aug 04 '16 at 17:27
  • Difficult to diagnostic. May be share how you defined your `x` and `y` data (I used `rand(25000,1)` to reproduce your example but may be different data distribution would stress the `png` renderer differently). Also are there any other elements in your figure ? (or is the figure just created automatically with the call to `scatter`) – Hoki Aug 04 '16 at 17:47
  • As another alternative, you can reproduce `scatter`'s behaviour with `plot` by `plot(x, y, 'b.');` This is assuming all points have the same colour and are also filled, which is what you are doing. Can you just use `plot` for now in the meantime? I'm not sure why `scatter` is taking a long time for you... perhaps it's because of R2012b and those issues have been ironed out with later releases of MATLAB. – rayryeng Aug 04 '16 at 18:25
  • @rayryeng Using `plot(x,y,'b.')` is actually a good idea for now. Thanks for the tip. By the way, I also tried `export-fig` & it took the same amount of time to save the image as the `scatter` function. – Bensa Aug 04 '16 at 21:06
  • Please make the code fully reproducible as people appear to struggle to reproduce the result. For instance, does the problem occur for `x=1:25000` and `y=x` ? I am also curious about the following: How long does it take for `x = 1:12500` ? – Dennis Jaheruddin Aug 05 '16 at 08:28
  • Sorry for the delay in response. I've attached a file with x1 & y1 created using `rand` function, and x2 & y2 which is my actual data. x1 & y1 takes less than 10 seconds to save the `scatter` plot as png, yet x2 & y2 takes 150+ seconds. I can't figure out how they are so different. http://www.filedropper.com/samp_2 – Bensa Aug 09 '16 at 19:39

2 Answers2

1

scatter is known to be slow for high number of datapoints; that might include saving them (each point has a difference color, thus needs separate handling).

You might want to try plotting the data differently, like described here: matlab: scatter plots with high number of datapoints This way, you get the same behaviour as with regular plots.

Gunther Struyf
  • 11,158
  • 2
  • 34
  • 58
0

Instead of calling saveas(), you could try calling getframe() and then calling imwrite(), like this:

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');    
Toothpick Anemone
  • 4,290
  • 2
  • 20
  • 42