Search code examples
matlabmatlab-figure

Passing colormap to scatter3


This is basic but I'm struggling to give scatter3 a colormap. I'm doing this:

scatter3(gesture_x(:,1),gesture_x(:,2),gesture_x(:,3),1,colors(labels_x))

Where colors = ['c','y','m'...] and labels_x = [1 3 3 2 ..]

If anyone could point out what I'm doing wrong that would be great.


Solution

  • You cannot use the single-character color specifications to specify an array of colors to be used for each point. MATLAB will actually interpret ['c', 'y', 'm'] as 'cym' and that isn't a valid color so it is going to error out.

    If you look at the documentation, you need to specify the color in one of three ways:

    • RGB values for each data point (an N x 3 array where the columns are red, green, and blue components),
    • A single color ('r' or 'red' or [1 0 0]) to be applied to all points,

    • A number which will be mapped to the colormap of the axes using the clims.

    Marker color, specified as a color string, an RGB row vector, a three-column matrix of RGB values, or a vector. For an RGB row vector, use a three-element row vector whose elements specify the intensities of the red, green, and blue components of the color. The intensities must be in the range [0 1]. If you have three points in the scatter plot and want the colors to be indices into the colormap, specify C as a three-element column vector.

    % Random RGB value for each point
    colors = rand(size(gesture_x, 1), 3);
    
    % One color for everything
    colors = 'r';
    colors = 'red';
    colors = [1 0 0];
    
    % Random values mapped to the axes colormap
    colors = rand(size(gesture_x,1), 1);