开发者

Plot data with MATLAB biplot with more than 1 color

I have 3 groups of data that had PCA performed on them as one group. I want to highlight each variable group with a different color. Prior to this I overlaid 3 biplots. This gives different colors but creates a distortion in the data as each biplot function skews the data. This caused the groups to all be skewed by different amounts, making the plot not a correct representation.

How do I take a PCA scores matrix (30x3开发者_高级运维) and split it so the first 10x3 is one color, the next 10x3 is another and the third 10x3 is another, without the data being skewed?


"Skewing" is happening because biplot is renormalizing the scores so the farthest score is distance 1 . axis equal isn't going to fix this. You should use scatter3 instead of biplot

data = rand(30,3);
group = scores(1:10,:)
scatter3(group(:,1), group(:,2), group(:,3), '.b')

hold all
group = scores(11:20,:)
scatter3(group(:,1), group(:,2), group(:,3), '.r')

group = scores(21:30,:)
scatter3(group(:,1), group(:,2), group(:,3), '.g')
hold off

title('Data')
xlabel('X')
ylabel('Y')
zlabel('Z')

Or modify your code's scatter3 lines so that the markers are different colors. The parameter after 'marker' tells what symbol and what symbol and color to plot. E.g. '.r' is a red dot. See Linespec for marker and color parameters.

scatter3(plotdataholder(1:14,1),plotdataholder(1:14,2),plotdataholder(1:14,3),35,[1 0 0],'marker', '.b');

hold on;

scatter3(plotdataholder(15:28,1),plotdataholder(15:28,2),plotdataholder(15:28,3),35,[0 0 1],'marker', '.r') ;

scatter3(plotdataholder(29:42,1),plotdataholder(29:42,2),plotdataholder(29:42,3),35,[0 1 0],'marker', '.g');


This is the method I used to plot biplot data with different colors. The lines of code prior to plot are taken from the biplot.m file. The way biplot manipulates data is kept intact and stops skewing of data when using overlaid biplots.

This coding is not the most efficient, one can see parts that can be cut. I wanted to keep the code intact so one can see how biplot works in it's entirety.

%%%%%%%%%%%%%%%%%%%%%

xxx = coeff(:,1:3); 
yyy= score(:,1:3);

**%Taken from biplot.m; This is alter the data the same way biplot alters data - having the %data fit on grid axes no larger than 1.**

[n,d2] = size(yyy);  
[p,d] = size(xxx); %7 by 3  
[dum,maxind] = max(abs(xxx),[],1);  
colsign = sign(xxx(maxind + (0:p:(d-1)*p)));  
xxx = xxx .* repmat(colsign, p, 1);  
yyy= (yyy ./ max(abs(yyy(:)))) .* repmat(colsign, 42, 1);  
nans = NaN(n,1);  

ptx = [yyy(:,1) nans]';  
pty = [yyy(:,2) nans]';  
ptz = [yyy(:,3) nans]';  

**%I grouped the pt matrices for my benefit** 

plotdataholder(:,1) = ptx(1,:);  
plotdataholder(:,2) = pty(1,:);  
plotdataholder(:,3) = ptz(1,:);  

**%my original score matrix is 42x3 - wanted each 14x3 to be a different color**

scatter3(plotdataholder(1:14,1),plotdataholder(1:14,2),plotdataholder(1:14,3),35,[1 0 0],'marker', '.');   
hold on;  
scatter3(plotdataholder(15:28,1),plotdataholder(15:28,2),plotdataholder(15:28,3),35,[0 0 1],'marker', '.') ;  
scatter3(plotdataholder(29:42,1),plotdataholder(29:42,2),plotdataholder(29:42,3),35,[0 1 0],'marker', '.');   
xlabel('Principal Component 1');  
ylabel('Principal Component 2');  
zlabel('Principal Component 3');  


I am not sure if it will help, but try axis equal after you have overlaid the plots.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜