File Exchange

fastscatter.m

version 1.3.0.0 (1.03 KB) by
A much faster scatterplot

Updated 29 Jul 2014

A fast scatter plot

h=fastscatter(X,Y,C [,markertype,property-value pairs])

Inputs:
X,Y: coordinates
C: color

Examples:
N=100000;
fastscatter(randn(N,1),randn(N,1),randn(N,1))

N=100;
fastscatter(randn(N,1),randn(N,1),randn(N,1),'+','markersize',7)
______________________________________
An illustrative example of the speed-up when using fastscatter over scatter:
N=200000;
X=randn(N,1);Y=randn(N,1);C=X.^2+Y.^2+randn(N,1);S=ones(N,1);
tic; for ii=1:10, clf; fastscatter(X,Y,C); drawnow; end; fastscatter_time=toc
tic; for ii=1:10, clf; scatter(X,Y,S,C); drawnow; end; regularscatter_time=toc
speed_increase=regularscatter_time/fastscatter_time

fastscatter_time = 3.4 seconds
regularscatter_time = 63.3 seconds
speed_increase = 18x

And for N=400000, the performance boost was ~38x.

Cite As

Aslak Grinsted (2021). fastscatter.m (https://www.mathworks.com/matlabcentral/fileexchange/47205-fastscatter-m), MATLAB Central File Exchange. Retrieved .

darova

Simple and fast. Thank you

This seems to be a very fast algorithm, excellent!

But I'm struggling with the color of the plot. I'm trying to pass a n by 3 matrix with rgb colors but the reordering that you make to the Z column work is changing the C column as well, so I'm allowed to pass only one color channel. How can I workaround this?

Greetings

Lockywolf

Unfortunately, this function doesn't work if the amount of points is 1 or 2.

fastscatter(randn(2,1),randn(2,1),randn(2,1),'+','markersize',7)
Error using mesh (line 70)
Z must be a matrix, not a scalar or vector.

Error in fastscatter (line 40)
h=mesh(X(ix),Y(ix),zeros(size(ix)),'marker',marker,'cdata',C(ix),'edgecolor','none','markeredgecolor','flat','facecolor','none',varargin{:});

Aslak Grinsted

@YounesN: i think this is caused by a bug in image for R2014b. Try this workaround:

image(A,'CDataMapping','scaled')

This function is excellent. The inbuilt scatter function was slowing my computer to a halt when I tried to plot > 200,000 points, but with fastscatter it's no problem. Thanks for sharing, Aslak!

Younes N

The speedup is amazing and helps a lot but I'm having difficulty with the colorbar when superimposing scatter points on an aerial image , using fastscatter() after image(). The colorbar doesn't correspond to the colors in the plot. Would highly appreciate your help.

MATLAB Release Compatibility
Created with R2014a
Compatible with any release
Platform Compatibility
Windows macOS Linux