MATLAB Answers

How can I color dots in a 3D Scatter Plot using a separate variable?

27 views (last 30 days)
I plotted a 3D score plot as follows:
>> scatter3(score(:,1),score(:,2),score(:,3));
How can I color the dots by an other variable (let us call it Y)?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 9 Aug 2019
You can achieve this by using the below syntax:
>> scatter3(score(:,1),score(:,2),score(:,3),[],Y)
The variable 'Y' has one of the following forms:
1. A three column matrix of RGB triplets — Use different colors for each marker. Each row of the matrix specifies an RGB triplet color for the corresponding marker. The number of rows must equal the length of 'score(:,1)', 'score(:,2)' and 'score(:,3)'.
Example: If you have three points in the scatter plot and want to specify the color corresponding to each marker, specify 'Y' as a three-column matrix of RGB triplets.
Y = [1 0 0 ; 0 1 0 ; 0 0 1]
2. Vector — Use different colors for each marker and linearly map values in 'Y' to the colors in the current colormap. The length of 'Y' must equal the length of 'score(:,1)', 'score(:,2)' and 'score(:,3)'. To change the colormap for the axes, use the 'colormap' function.
Example: If you have three points in the scatter plot and want the colors to be indices into the colormap, specify 'Y' as a three-element column vector.
Y = [1, 2, 3]
You can find an example to vary 'marker color' in a 3D scatter plot in the documentation link below:
In the above example, the vector 'c' corresponds to the 'marker color' and vector 's' corresponds to the 'marker area'.
You can find additional information about these input arguments in the below link:


Sign in to comment.

More Answers (0)


No tags entered yet.




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!