Coloring The Dots in biPlot Chart

조회 수: 21 (최근 30일)
Yaser Khojah
Yaser Khojah 2019년 4월 25일
댓글: ALESSANDRO D'ALESSANDRO 2020년 12월 1일
I have created biplot as below and I'm looking for a way to distinguish the dots by different colors according to their group name. There are 12 groups and here are mydata and codes.
PC1andPC2.png
categories = ['F1';'F2';'F3';'F4';'F5';'F6';'F7';'F8'];
load('MAT_ALL.mat')
figure(1)
[coefforth,score,~,~,explainedVar] = pca(MaT_All(:,9:16));
load('DataGroup.mat')
clusters = DataGroup(:,20);
[coefforth,score,~,~,explainedVar] = pca(MaT_All(:,9:16));
figure(3)
biplot([coefforth(:,1) coefforth(:,2)],'Scores',[score(:,1) score(:,2)],'Varlabels',categories);

채택된 답변

Adam Danz
Adam Danz 2019년 4월 25일
편집: Adam Danz 2019년 4월 25일
The biplot() function has an output that lists handles to all objects in the plot. All you need to do is isolate the handles to the scatter points by referencing the handle tags and then assign color based on the category.
If you have any questions, feel free to leave a comment.
% Your code
categories = ['F1';'F2';'F3';'F4';'F5';'F6';'F7';'F8'];
load('MAT_ALL.mat')
% figure(1) (No need for this)
[coefforth,score,~,~,explainedVar] = pca(MaT_All(:,9:16));
load('DataGroup.mat')
clusters = DataGroup(:,20);
[coefforth,score,~,~,explainedVar] = pca(MaT_All(:,9:16));
figure()
% Store handle to biplot
h = biplot([coefforth(:,1) coefforth(:,2)],'Scores',[score(:,1) score(:,2)],'Varlabels',categories);
% Identify each handle
hID = get(h, 'tag');
% Isolate handles to scatter points
hPt = h(strcmp(hID,'obsmarker'));
% Identify cluster groups
grp = findgroups(clusters); %r2015b or later - leave comment if you need an alternative
grp(isnan(grp)) = max(grp(~isnan(grp)))+1;
grpID = 1:max(grp);
% assign colors and legend display name
clrMap = lines(length(unique(grp))); % using 'lines' colormap
for i = 1:max(grp)
set(hPt(grp==i), 'Color', clrMap(i,:), 'DisplayName', sprintf('Cluster %d', grpID(i)))
end
% add legend to identify cluster
[~, unqIdx] = unique(grp);
legend(hPt(unqIdx))
You can select a different color map (I'm using 'lines'). : https://www.mathworks.com/help/matlab/ref/colormap.html#buc3wsn-1-map190425 094457-Figure 1.jpg
  댓글 수: 11
Adam Danz
Adam Danz 2020년 11월 29일
You need to keep track of your random permutation indices and apply the same permutation to the species vector.
randIdx = randperm(size(iris, 1));
irisRandom = iris(randIdx, :);
species = species(randIdx);
ALESSANDRO D'ALESSANDRO
ALESSANDRO D'ALESSANDRO 2020년 12월 1일
thanks!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by