Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
scatter3 different colours for density

Subject: scatter3 different colours for density

From: J G

Date: 29 Aug, 2011 04:36:10

Message: 1 of 7

I'm using scatter3 however it does not represent density very well. Is it possible to plot different colours to represent different densities in scatter3? I think there is a function to do this in 2D on the file exchange, however I'm pretty new to matlab and don't know how to do this in 3D. Thanks.

Subject: scatter3 different colours for density

From: Bruno Luong

Date: 29 Aug, 2011 06:15:14

Message: 2 of 7

"J G" wrote in message <j3f4vq$di3$1@newscl01ah.mathworks.com>...
> I'm using scatter3 however it does not represent density very well. Is it possible to plot different colours to represent different densities in scatter3? I think there is a function to do this in 2D on the file exchange, however I'm pretty new to matlab and don't know how to do this in 3D. Thanks.

This function can build histogram in any dimension
http://www.mathworks.com/matlabcentral/fileexchange/23897-n-dimensional-histogram

Bruno

Subject: scatter3 different colours for density

From: Steven_Lord

Date: 29 Aug, 2011 13:34:11

Message: 3 of 7



"J G" <jemma.geoghegan@gmail.com> wrote in message
news:j3f4vq$di3$1@newscl01ah.mathworks.com...
> I'm using scatter3 however it does not represent density very well. Is it
> possible to plot different colours to represent different densities in
> scatter3? I think there is a function to do this in 2D on the file
> exchange, however I'm pretty new to matlab and don't know how to do this
> in 3D. Thanks.

Use the C input to SCATTER3.

http://www.mathworks.com/help/techdoc/ref/scatter3.html

Run the example on that page, then type:

colorbar

Those points corresponding to 1's in C are colored in blue, the color
associated with 1 in the colorbar. Similarly, the middle shell of points
corresponds to 2's in C and are green, the color associated with 2 in the
colorbar and the outer points are 3's or red.

You can build your own C that corresponds to whatever property of your
points that you want.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: scatter3 different colours for density

From: J G

Date: 30 Aug, 2011 05:38:10

Message: 4 of 7

"Steven_Lord" <slord@mathworks.com> wrote in message <j3g4gj$art$1@newscl01ah.mathworks.com>...
>
>
> "J G" <jemma.geoghegan@gmail.com> wrote in message
> news:j3f4vq$di3$1@newscl01ah.mathworks.com...
> > I'm using scatter3 however it does not represent density very well. Is it
> > possible to plot different colours to represent different densities in
> > scatter3? I think there is a function to do this in 2D on the file
> > exchange, however I'm pretty new to matlab and don't know how to do this
> > in 3D. Thanks.
>
> Use the C input to SCATTER3.
>
> http://www.mathworks.com/help/techdoc/ref/scatter3.html
>
> Run the example on that page, then type:
>
> colorbar
>
> Those points corresponding to 1's in C are colored in blue, the color
> associated with 1 in the colorbar. Similarly, the middle shell of points
> corresponds to 2's in C and are green, the color associated with 2 in the
> colorbar and the outer points are 3's or red.
>
> You can build your own C that corresponds to whatever property of your
> points that you want.
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com


Thanks for your replies.
Steve, thanks for that - I'm still having a bit of difficulty defining C to correspond to the density of my points. I figure I need to do some sort of count to my data to see how many times a particular set of coordinates occur? Any suggestions how to do this would be great (sorry I'm clueless).

This is my code so far...
(My data are 9x250 matrix defined in d)

d = data;
x1 = d(:,1);
y1 = d(:,2);
z1 = d(:,3);
x2 = d(:,4);
y2 = d(:,5);
z2 = d(:,6);
x3 = d(:,7);
y3 = d(:,8);
z3 = d(:,9);

S = repmat([1 .75 .5]*10,numel(x1),1);
C = repmat([1 2 3],numel(x1),1); %obviously wrong!

scatter3(x1,y1,z1,S(:,1),C(:,1))
hold on
scatter3(x2,y2,z2,S(:,2),C(:,2))
scatter3(x3,y3,z3,S(:,3),C(:,3))

colorbar

Subject: scatter3 different colours for density

From: Bruno Luong

Date: 30 Aug, 2011 06:01:26

Message: 5 of 7

Use the tool on FEX I indicate earlier:

X=randn(5000,3);

edge = linspace(-2,2,10); % change the edge definition suitable to your data range
[count, ~, ~, loc] = histcn(X,edge,edge,edge); % FEX
% Compute the density
density= zeros(size(X,1),1);
valid = all(loc,2);
loc = loc(valid,:);
density(valid) = count(sub2ind(size(count),loc(:,1),loc(:,2),loc(:,3)));

scatter3(X(:,1),X(:,2),X(:,3),5,density)

% Bruno

Subject: scatter3 different colours for density

From: Bruno Luong

Date: 30 Aug, 2011 06:43:10

Message: 6 of 7

With a Gaussian filtering, the color transition is smoother:

% Data
X=randn(5000,3);

% Compute the histogram
edge = linspace(-2,2,50); % change here to your need;
[count, ~, ~, loc] = histcn(X,edge,edge,edge);

% Gaussian smoothing the histogram
kernel = exp(-linspace(-2,2,11).^2);
K = 1;
for k=1:3
    K = K(:)*kernel;
end
K = reshape(K,length(kernel)+[0 0 0 ]);
K = K/sum(K(:));
count = convn(count, K,'same');

% Density
density= zeros(size(X,1),1);
valid = all(loc,2);
loc = loc(valid,:);
density(valid) = count(sub2ind(size(count),loc(:,1),loc(:,2),loc(:,3)));

% Plot
scatter3(X(:,1),X(:,2),X(:,3),5,density)
colormap(hot)

% Bruno

Subject: scatter3 different colours for density

From: J G

Date: 30 Aug, 2011 06:58:10

Message: 7 of 7

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <j3i0pu$hli$1@newscl01ah.mathworks.com>...
> With a Gaussian filtering, the color transition is smoother:
>
> % Data
> X=randn(5000,3);
>
> % Compute the histogram
> edge = linspace(-2,2,50); % change here to your need;
> [count, ~, ~, loc] = histcn(X,edge,edge,edge);
>
> % Gaussian smoothing the histogram
> kernel = exp(-linspace(-2,2,11).^2);
> K = 1;
> for k=1:3
> K = K(:)*kernel;
> end
> K = reshape(K,length(kernel)+[0 0 0 ]);
> K = K/sum(K(:));
> count = convn(count, K,'same');
>
> % Density
> density= zeros(size(X,1),1);
> valid = all(loc,2);
> loc = loc(valid,:);
> density(valid) = count(sub2ind(size(count),loc(:,1),loc(:,2),loc(:,3)));
>
> % Plot
> scatter3(X(:,1),X(:,2),X(:,3),5,density)
> colormap(hot)
>
> % Bruno


Works great! Thank you so much for your help!

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us