Coloring The Dots in biPlot Chart
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
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.

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);
採用された回答
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-map

11 件のコメント
Yaser Khojah
2019 年 4 月 25 日
Dear Adam
Thank you so much for writing this code. I totally appreciate for your help. You really made my day. I have a further question regarding this problem. I wanted to create scatter plot for PC1 and 2 scores while showing their clusters as below but it is not working. Is there any way to help on this please.
figure(2)
gscatter(score(:,1),score(:,2),clusters)
Hi Yaser, I'm glad I could help.
The reason your line of code above isn't working is because 'clusters' is a table and gscatter() doesn't like tables.
You could use my grp variable instead or pull the data from the table.
figure(2)
gscatter(score(:,1),score(:,2),grp)
% OR
gscatter(score(:,1),score(:,2),clusters.Variables)
Yaser Khojah
2019 年 4 月 25 日
編集済み: Yaser Khojah
2019 年 4 月 25 日
Dear Adam,
Earlier I have tried something like the below and it worked. Please note cul and clusters have the same formate.
[clu, centroid] = kmeans(score(:,1:2),12);
figure(2)
gscatter(score(:,1),score(:,2),clu)
Now I tried yous and it is working. I'm just wondering what is the different but grp and clusters since I see they have the same formate. Thank you so so so much
Your variable "clusters" is a table that contains cluster number 1 to 11 and a bunch of NaN values. You mentioned that you have 12 clusters so I interpreted the NaNs as cluster #12.
My grp is a grouping variable of numbers 1 to 12 where 1:11 match your cluster 1:11 and the 12s represent the NaNs in 'clusters'.
I recommend going through every line of my code to make sure it's doing what you want it to do. There's only 7-8 lines to look at. If you have any questions, feel free to ask.
Yaser Khojah
2019 年 4 月 26 日
Dear Adam, thank you for all your help. It is working perfect as I want it. What is your recommendation since I have a lot of data and sometimes the computer freezes when I like to change something manually such as the marker size?
Adam Danz
2019 年 4 月 26 日
I know what that frustration feels like and I wish I had a solution. Here are a few suggestions.
1) when you're developing algorithms, plots, etc, use a much smaller dataset - even if it's made of fake data. That way the trial-and-error process isn't impeded by loading and processing large data. When your code is perfected, then load the real data and test it.
2) (this one's obvious) Use a machine with more RAM or a better processor.
3) Search your code for inefficiencies. Using the profile() function, you can time the execution of your code and it can produce a report that lists execution time for each function.
4) Using remote desktop and a second monitor, often times I'll run matlab on two machines and go back and forth, working on whichever one isn't busy.
Yaser Khojah
2019 年 4 月 26 日
Thanks for all these tips.
My response:
h = bipolot(__);
set(h, 'Marker', 'o')
ALESSANDRO D'ALESSANDRO
2020 年 11 月 29 日
Hi Adam, your suggestion was very good.
In my case, the groping variables are not ordered and I can't find the way to assign them in the proper order.
Below just an example to show you what I mean.
Do you have any suggestion?
Thanks
clear; clc;
load iris.dat;
species = grp2idx(iris(:,5));
[n_row,n_col] = size(iris);
irisRandom = iris(randperm(size(iris, 1)), :);
[loadings,scores] = pca(irisRandom(:,1:3));
% uniqueVarieta = unique(species);
% Store handle to biplot
h = biplot(loadings(:,1:2),...
'Scores',scores(:,1:2));
% Identify each handle
hID = get(h, 'tag');
% Isolate handles to scatter points
hPt = h(strcmp(hID,'obsmarker'));
% Identify cluster groups
grp = findgroups(species);
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
clrMap = lines(length(unique(species))); % using 'lines' colormap
for i = 1:max(grp)
set(hPt(grp==i), 'Color', clrMap(i,:), 'DisplayName', sprintf('species %d', grpID(i)))
end
% add legend to identify cluster
[~, unqIdx] = unique(grp);
legend(hPt(unqIdx))
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
2020 年 12 月 1 日
thanks!
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Solver Outputs and Iterative Display についてさらに検索
タグ
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
