Search code examples
matlabscatter-plotmatlab-figurecolor-coding

3D scatterplot colored by Z-Value


I've been googling for a while but couldn't find a solution for my problem. I am an amateur matlab user and I would like to create a 3D scatterplot, for this I have a matrix containing several points in 3D space:

>> size(A)

  ans =

        2511           3

I was able to create a 3D scatterplot using "scatter3" function, but now I am stuck a bit at color-coding the 3D points.

scatter3(A(:,1),A(:,2),A(:,3));

This will plot the data, but now I would like to add a color coding based on the z-Value... The colors themself don't matter too much. It could be a rainbow spectrum or a temperature spectrum or whatever. I just would like to colorcode them to distinguish the z-Values of the points.

Can anybody help me with this? Thank you!


Solution

  • You have to give some more arguments to scatter3.

    scatter3(X,Y,Z,S,C);
    

    S lets you specify areas for each markers (with a vector) or a single area for all the markers, while C lets you specify color. If C is a vector, its values will be linearly mapped to the current colormap. To change the colormap, call colormap(jet) for example. See the documentation on colormap.

    Sorry if that's confusing. Short version:

    scatter3(A(:,1),A(:,2),A(:,3),9,A(:,3));
    colormap(jet); %# or other colormap