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

MathWorks Support Team (view profile)

on 8 Aug 2019
Latest activity Answered by MathWorks Support Team

MathWorks Support Team (view profile)

on 9 Aug 2019
Accepted Answer by MathWorks Support Team

MathWorks Support Team (view profile)

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)?

Tags

No tags entered yet.

R2018b

MathWorks Support Team (view profile)

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'.