将colormap传递给scatter3

时间:2016-06-13 12:52:25

标签: matlab matlab-figure

这是基本的,但我很难给scatter3(gesture_x(:,1),gesture_x(:,2),gesture_x(:,3),1,colors(labels_x)) 一个色彩图。我这样做:

colors = ['c','y','m'...]

labels_x = [1 3 3 2 ..]Select()

的位置

如果有人能指出我做错了什么就会很棒。

2 个答案:

答案 0 :(得分:0)

您不能使用单字符颜色规范来指定要用于每个点的颜色数组。 MATLAB实际上会将['c', 'y', 'm']解释为'cym',这不是有效的颜色,因此会出错。

如果查看the documentation,则需要以三种方式之一指定颜色:

  • 每个数据点的RGB值(列为红色,绿色和蓝色组件的N x 3数组),
  • 要应用于所有点的单一颜色('r''red'[1 0 0]),

  • 使用clims映射到轴色图的数字。

  

标记颜色,指定为颜色字符串,RGB行向量,RGB值的三列矩阵或向量。对于RGB行向量,使用三元素行向量,其元素指定颜色的红色,绿色和蓝色分量的强度。强度必须在[0 1]范围内。如果散点图中有三个点并希望颜色成为色图的索引,请将C指定为三元素列向量。

% 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);

答案 1 :(得分:0)

此答案通过一个完整的示例补充了出色的现有answer,并使用了从this post的答案中获得的信息。

我发现使用scatter3来应用颜色的最简单方法是通过colormap

从文档中(重点是我的):

  

scatter3(X,Y,Z,S,C)用C指定的颜色绘制每个圆。

     

如果C是RGB三元组或包含颜色的字符向量或字符串   名称,然后以指定的颜色绘制所有圆。

     

如果C是三列矩阵,且C中的行数等于   X,Y和Z的长度,那么C的每一行都指定RGB颜色   相应圆的值。

     

如果C是一个长度等于X,Y和Z的向量,则    C中的值线性映射到当前颜色   颜色图。

下面提供了带有自定义功能的完整示例(标记大小控制除外)。 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  
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')
相关问题