MATLAB Answers

0

Coloring The Dots in biPlot Chart

Asked by Yaser Khojah on 25 Apr 2019
Latest activity Commented on by Yaser Khojah on 26 Apr 2019
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);

  0 Comments

Sign in to comment.

Tags

1 Answer

Answer by Adam Danz
on 25 Apr 2019
Edited by Adam Danz
on 25 Apr 2019
 Accepted Answer

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

  7 Comments

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
on 26 Apr 2019
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.
Thanks for all these tips.

Sign in to comment.