MATLAB: Color Different Scatter Points Based on Group

3d plotscolorgscatterscatterStatistics and Machine Learning Toolbox

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!

Best Answer

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