No License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from

4.1 | 19 ratings Rate this file 74 Downloads (last 30 days) File Size: 6.34 KB File ID: #8577 Version: 1.0
image thumbnail



Alex Sanchez (view profile)


27 Sep 2005 (Updated )

Scatter plot with color indicating data density.

| Watch this File

File Information

% Scatter plot with color indicating data density
% out = scatplot(x,y,method,radius,N,n,po,ms)
% out = scatplot(x,y,dd)
% Draws a scatter plot with a colorscale
% representing the data density computed
% using three methods
% x,y - are the data points
% method - is the method used to calculate data densities:
% 'circles' - uses circles with a determined area
% centered at each data point
% 'squares' - uses squares with a determined area
% centered at each data point
% 'voronoi' - uses voronoi cells to determin data densities
% default method is 'voronoi'
% radius - is the radius used for the circles or squares
% used to calculate the data densities if
% (Note: only used in methods 'circles' and 'squares'
% default radius is sqrt((range(x)/30)^2 + (range(y)/30)^2)
% N - is the size of the square mesh (N x N) used to
% filter and calculate contours
% default is 100
% n - is the number of coeficients used in the 2-D
% running mean filter
% default is 5
% (Note: if n is length(2), n(2) is tjhe number of
% of times the filter is applied)
% po - plot options:
% 0 - No plot
% 1 - plots only colored data points (filtered)
% 2 - plots colored data points and contours (filtered)
% 3 - plots only colored data points (unfiltered)
% 4 - plots colored data points and contours (unfiltered)
% default is 1
% ms - uses this marker size for filled circles
% default is 4
% out - structure array that contains the following fields:
% dd - unfiltered data densities at (x,y)
% ddf - filtered data densities at (x,y)
% radius - area used in 'circles' and 'squares'
% methods to calculate densities
% xi - x coordenates for zi matrix
% yi - y coordenates for zi matrix
% zi - unfiltered data densities at (xi,yi)
% zif - filtered data densities at (xi,yi)
% [c,h] = contour matrix C as described in
% CONTOURC and a handle H to a contourgroup object
% hs = scatter points handles

MATLAB release MATLAB 7 (R14)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (31)
18 Feb 2017 shantanaba majumder

17 Feb 2017 Xiangping Hu

Hi, there is a color bar after applied the scatplot What does the colorbar mean here?

Comment only
24 Dec 2016 tirebird

great one!

01 Dec 2016 Giuseppe Brunello

I made a change to make it run faster with the 'circle' option for high number of points.

Instead of comparing the euclidean distance, I compare the square eucledian distance, saving a sqrt calculation:


case 'ci'
for k=1:Ld
dd(k) = sum( sqrt((x-x(k)).^2 + (y-y(k)).^2) < r );
area = pi*r^2;
dd = dd/area;

Now reads:

case 'ci'
SqR = r*r;
for k=1:Ld
dd(k) = sum( (x-x(k)).^2 + (y-y(k)).^2 < SqR );
area = pi*r^2;
dd = dd/area;

Note, that I only need one square evaluation and I dropped all the sqrt evaluations (w/ a million data points, that's a million evaluations)

04 Nov 2016 Mohsen Naji

05 Sep 2016 Margaux Silvestre

I have the same question as mimi and Aolin Jia.
what represent the 0 and the 60 in the colorbar? Is it possible to have it in term of density?
Is it possible to change the scale of the colorbar?
Otherwise, it is all I needed. Thank you.

Comment only
09 Aug 2016 Imene

Imene (view profile)

Is there a functio for a 3D plot?

13 Apr 2016 george saman

Takes forever to plot , if you have too many data.

Comment only
22 Jan 2016 Erik S.

22 Nov 2015 mimi

mimi (view profile)

I do have the same question as of Aolin Jia. I want a uniform colorbar distribution between; may I know how to change this?

Comment only
21 Nov 2015 Aolin Jia

The highest value in the colorbar is always 60 and the lowest is always 0. How can this be changed?

27 Oct 2015 Pearl

Pearl (view profile)

just what I needed! Thank you!

17 Apr 2015 Sevian

Sevian (view profile)

Im having problems using the 'voronoi' method. I am getting an error saying duplicate data values were found. But works well with 'circles'. Also i cant have a colourful plot of the points with contour lines. Can anyone help me out please ?

Comment only
16 Jan 2015 Daniel

Daniel (view profile)

What do the numbers on the colorbar scale actually mean?

Comment only
04 Dec 2014 Jo Williams

I liked the idea behind the gsp subfunction, and it *is* much more efficient than scatter. I tried this section on its own. But it should be used with care - the order that you work through the colormap will affect the final plot, with later (default red) colours overlaid over earlier (default blue) wherever the dots are crowded close together. Especially if you shrink the figure this can result in a massive bias towards (default) red. To test the effect on your plot, try replacing the loop with

for k=size(map,1):-1:1

Comment only
04 Dec 2014 Syed

Syed (view profile)

Why the data density is fixed. The highest value in the colorbar is always 60 and the lowest is always 0. How can this be changed?

Comment only
01 Aug 2014 Aidan O'Farrell

Is there a way to identify the highest density position in the output variables?

Comment only
03 Jun 2014 Ellisa Dewi

can you show us the example code?

28 Feb 2014 ocean

ocean (view profile)

11 Feb 2014 M

M (view profile)

That`s very useful, thanks!

27 Dec 2013 Right Grievous

I just get an error saying duplicate data values were found and removed - the figure produced is empty. I thought the point of this script was to handle duplicate values as it is showing density?

Comment only
25 Apr 2013 Walter Guy

Definition of sub-function range() is missing (no add-on toolboxes), also several missing end statements for sub-functions. Once these errors are corrected it performs well. (R2012a)

20 Jan 2013 Right Grievous

Great piece of code - just what I needed! However, I was wondering if there is anyway to set the colors? i.e. like how you can set a colorbar limits in other matlab plotting tools. I can't seem to find a way.

I have one large density peak which sets the colorbar so high nothing else really shows up...

Comment only
29 Nov 2012 Patroklus

Very nice job! Very useful.

15 Feb 2011 Hongxiao Jin

%this works fine for simple show
function hh=scatterC(varargin,n,method)
if nargin<2
n = 100; %bins
if nargin<3

[N C]=hist3(varargin,[n,n]);
[X Y]=meshgrid(C{1},C{2});
switch method
case 'p'
pcolor(X,Y,N);shading flat;
case 'c' %contourf

Comment only
28 Jan 2008 Garret Duffy

Excellent and useful plotting utility. Specification of command-line parameters is tricky however. Perhaps a runtime feedback of what input parameters were specified would be useful.

02 Jun 2007 New Sung

It's what I am just looking for. Very nice! Thank you.

18 Apr 2007 John D'Errico

A very nice utility for visualizing scattered data in 2-d. Excellent help, an H1 line, defaults for the variables, internal comments, etc.

A flaw is there is no error checking done on the parameters for viability. For example, the method argument is described as taking on one of 'circles', 'squares', or 'voronoi'. However, the code checks only the first two characters, both of which must be correct. This feature is undocumented. Note that strcmpi or strmatch are easy to use tools that will check for any shortened versions of the options. You can even ignore capitalization.

Similarly, there is no error checking on the po (plot options) argument. If you pass in a 5, you get an arbitrary result.

I would have liked to give this a 5 rating, and will happily raise my rating.

03 May 2006 J C

Nice work. thank you !

03 Dec 2005 Birajashis Pattnaik

Ya simple but impressive,Good

18 Nov 2005 m2 howard

nice, simple, and effective!

Contact us