Keeping same colors for the same groups in different group scatter plot

9 次查看(过去 30 天)
Hi!
I have a set of data containing around 5 000 000 different datapoints and these have been grouped into four different groups with the help of k-means clustering. When I plot these using gscatter, the four different colors presenting the datapoints belonging to each group in the plot are : group 1: purple, 2: blue, 3: orange and 4: yellow.
However, when I want to plot, for example, the first 100 000 of the datapoints they might only contain data belonging to group 1 and 3. Then the plot colours these as group 1: purple and group 3: blue instead of orange as in the original plot. How can I make it so the plots colours the datapoints based on the group they belong to?
Thank you in advance!

采纳的回答

KSSV
KSSV 2023-3-17
P = rand(1000,2) ;
x = P(:,1) ; y = P(:,2) ;
idx = kmeans(P,4) ;
figure
h = gscatter(x,y,idx) ;
% Get colors
C = reshape([h.Color],3,[])' ;
figure
hold on
scatter(x(idx==1),y(idx==1),[],C(1,:))
scatter(x(idx==2),y(idx==2),[],C(2,:))
  2 个评论
Oliver Wallin
Oliver Wallin 2023-3-20
It does not seem to work if i do not want to plot every value that belongs to one group at the same time.
I have it like this:
x = data(:,1) ; y = data(:,2) ; z = data(:,3) ; w = data(:,4) ;
grp = kmeans(data,4);
% Then I divide all datapoints into colums with equal ammount of values
h = 0;
for i = 1:length(data)
g = 100*i;
x1(:,i) = x(h+1:g);
y1(:,i) = y(h+1:g);
z1(:,i) = z(h+1:g);
w1(:,i) = w(h+1:g);
grp1(:,i) = grp(h+1:g);
h = 100*i
end
% Then I plot column by column for different variables, and this is one
% example
for j = 1:5
figure
gscatter(x1(:,j),y1(:,j),grp1(:,j))
end
So when I plot these values i want the colors to remain by group number by column. Because the first column might contain values from all four groups and the second column might contain from only 2 groups and so on.
Oliver Wallin
Oliver Wallin 2023-3-20
I solved that issue by creating new arrays in the plot loop. So now it works
Thanks for the help!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Scatter Plots 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by