1

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.

Suever
  • 64,497
  • 14
  • 82
  • 101
chris
  • 4,840
  • 5
  • 35
  • 66

2 Answers2

0

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);
Suever
  • 64,497
  • 14
  • 82
  • 101
0

This answer complements the excellent existing answer with a full example and uses information gained from the answer at this post.

I find the easiest way to apply color using scatter3 is through the colormap.

From the documentation (emphasis mine):

scatter3(X,Y,Z,S,C) draws each circle with the color specified by C.

If C is a RGB triplet or character vector or string containing a color name, then all circles are plotted with the specified color.

If C is a three column matrix with the number of rows in C equal to the length of X, Y, and Z, then each row of C specifies an RGB color value for the corresponding circle.

If C is a vector with length equal to the length of X, Y, and Z, then the values in C are linearly mapped to the colors in the current colormap.

Full example provided below with customization (except for marker size control). 3D scatter plot with colormap

% MATLAB R2017a 
% Data
NumPoints = 25;
X = 100*rand(NumPoints,1);
Y = 100*rand(NumPoints,1);
Z = 100*rand(NumPoints,1);
V = (X + Y);

% Create custom colormap     (2 color example)
col1 = [0 1 0]; %G
col2 = [1 0 0]; %R
cmap = interp1([col1; col2], linspace(1, 2, 101)); % Create the colormap

% Plot
colormap(cmap), hold on, box on
h = scatter3(X,Y,Z,[],V,'filled')
view(-25,25)   % (azimuth,elevation)

% Colorbar Controls
cb = colorbar;
caxis([0 200])    % sets min and max value for color mapping (separate from tick control)
cb.Limits = [0 200];
cb.Ticks = [0:25:200];  % custom set colorbar ticks
cb.Label.String = ' V';
cb.Label.Rotation = 0;   % Default is 90

% Cosmetics
h.MarkerEdgeColor = 'k';     % Set marker edge color to black
xlabel('X')
ylabel('Y')
zlabel('Z')
SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41