MATLAB: How to associate the markers of the SCATTER3 plot with a color map in MATLAB 7.10 (R2010a)

MATLABpoints

I would like to control the color of my SCATTER3 markers with a COLORMAP.

Best Answer

To control how your SCATTER3 markers are colored you can make use of the CData property of your graphic object.
CData controls the color of markers. If you would like to map your points with a colormap you can use a vector with the same length of your XData and YData then the values in CData are linearly mapped to the colors in the current colormap.
Please consider the following code as an example:
%%Define data
[x,y,z] = sphere(16);
X = [x(:)*.5 x(:)*.75 x(:)];
Y = [y(:)*.5 y(:)*.75 y(:)];
Z = [z(:)*.5 z(:)*.75 z(:)];
%%Create Scatter3 plot with CDATA value Z
scatter3(X(:),Y(:),Z(:),'CData',Z(:))
colorbar
In this case the Z value has been used to define the colormap but you can consider any other different map you may like by inserting a different vector with the same size of X and Y.