Color Different Scatter Points Based on Group

Hello,
I have a great deal of points on a 3D scatter plot that I need to be shown in different colors. They need to be categorized in color based on a label that they are given in the data set. All examples I have seen for coloring markers in scatter plots has been based on distance/position (which is not what I need).
Any help with how to code color markers based on grouping/labeling would be appreciated!

 Réponse acceptée

[unique_groups, ~, group_idx] = unique(Your_Group_Labels);
num_groups = size(unique_groups, 1);
if isnumeric(unique_groups)
group_names = cellstr( num2str( unique_groups ) );
elseif ischar(unique_groups)
group_names = mat2cell( unique_groups, ones(1, num_groups), size(unique_groups, 2) );
elseif iscell(unique_groups)
group_names = unique_groups;
else
error('I do not know how to convert group labels of class "%s" to printable strings', class(unique_groups));
end
pointsize = 30;
scatter3(x, y, z, pointsize, group_idx);
cmap = jet(num_groups); %or build a custom color map
colormap( cmap );
legend(group_names);
If only you knew ahead of time what the data class of your labels was going to be, then you could simplify your code...

4 commentaires

Brian Tomblin
Brian Tomblin le 28 Juin 2016
Modifié(e) : Brian Tomblin le 28 Juin 2016
I am unsure what you mean exactly with data class of the labels, but they are text names of muscles in a cell array, if that helps. The idea is to color-separate the XYZ coordinates based on their generated labeled muscle.
For the case of the labels being cell array of strings:
[unique_groups, ~, group_idx] = unique(Your_Group_Labels);
num_groups = size(unique_groups, 1);
pointsize = 30;
scatter3(x, y, z, pointsize, group_idx);
cmap = jet(num_groups); %or build a custom color map
colormap( cmap );
legend(unique_groups);
If I already have a legend with corresponding labels and colors produced by the original code, can that be used more easily in the scatter3?
If you have a cell array of (unique) labels, and an N x 3 RGB array of corresponding color specifications, then the easiest way would be to sort() the (unique) label names and pull out the sorting order (second output of sort()) and use that order to rearrange the color table rows to create the cmap variable above, and also use the order to re-arrange the legends. To phrase that a different way, you should create a colormap variable whose rows are in the same order as the alphanumeric sort order as the labels themselves, and likewise a rearranged legend
[~, sortidx] = sort(label_name_table);
ordered_cmap = corresponding_color_table(sortidx, :);
ordered_legends = corresponding_legend_entries(sortidx);
[~, ~, group_idx] = unique(Your_Group_Labels);
pointsize = 30;
scatter3(x, y, z, pointsize, group_idx);
colormap( ordered_cmap );
legend( ordered_legends );

Connectez-vous pour commenter.

Plus de réponses (1)

Chad Greene
Chad Greene le 24 Juin 2016
Similar to Walter's solution:
% define 25 random points:
x = randn(25,1);
y = randn(25,1);
% Each x,y point is in group 1, group 2, or group 3:
group = randi(3,25,1);
% Plot:
plot(x(group==1),y(group==1),'.','markersize',20,'color','green');
hold on
plot(x(group==2),y(group==2),'.','markersize',20,'color','red');
plot(x(group==3),y(group==3),'.','markersize',20,'color','black');
legend('group 1','group 2','group 3')

2 commentaires

If you have 2D data then you can use gscatter()
Chad Greene
Chad Greene le 26 Juin 2016
Ah, yes, I forgot about that function!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Data Distribution Plots dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by