Reputation: 5559
Is there a way to control the colours in scatter3
?
In my clustering problem I have 2 vectors
A
= 80x3 containing my data
and index
containing the indices of the clusters. So for example
the data point A(i,1) A(i,2) A(i,3)
belongs to the cluster index(i)
.
With scatter3(A(:,1),A(:,2),A(:,3),50,index','filled')
I plot all the data-points according to their clusters, but I would like to specify the colour for each group of points.
I tried with the Help, but I didn't manage.
Upvotes: 3
Views: 3389
Reputation: 7751
You are nearly there. scatter3
provides several options to define the color scheme (see the doc here). One is indexing with scalars as you do right now. The coloring scheme is automatic in this case. Another option is to use triplets of values representing RGB colors.
For instance, for three colors you can define a custom colormap cmap
.
color_1 = [1 0.2 0.4];
color_2 = [0.34 0.65 0.87];
color_3 = [0.5 0.5 0.5];
cmap = [color_1; color_2; color_3];
and then create an INDEX_color
(Nx3) matrix based on your actual indexing
INDEX_color = cmap(INDEX,:);
For more groups, you can automatically create INDEX_color
based on buil-in colormaps. cmap = colormap(jet(10));
will produce a 10x3
RGB matrix folowwing the jet
colormap.
The following figure
is given by this code
A = [rand(20,3); rand(20,3)+1; rand(20,3)+2 ];
INDEX = [ones(20,1); ones(20,1)+1; ones(20,1)+2 ];
color_1 = [1 0.2 0.4];
color_2 = [0.34 0.65 0.87];
color_3 = [0.5 0.5 0.5];
cmap = [color_1; color_2; color_3];
INDEX_color = cmap(INDEX,:);
scatter3(A(:,1), A(:,2), A(:,3), 50, INDEX_color, 'filled')
Upvotes: 4