multiple scatter plot

6 Ansichten (letzte 30 Tage)
Hassan
Hassan am 23 Apr. 2011
I have 4 arrays, A,B,C,D which their size is 80 rows, 4 columns. I want to make a scatter plot between the same column of each two arrays e.g. X=A(:,1),Y=B(:,1) or X=C(:,4), Y=D(:,4). I have a cell array s={s1;s2,...;s16} which s1 is lable for A(:,1), if X=A(:,1) then the lable for x axis is s1 and if Y=A(:,1)then the lable for y axis is s1. I also want to add the regression equation and r-squared value. I used the following code but it's not working. ANy help is appericiated.
s={s1;s2;s3;s4;s5;s6;s7;s8;s9;s10;s11;s12;s13;s14;s15;s16}
% to draw a 1:1 line
fignum = 1;
for i=1:4
while ishandle(fignum)
fignum = fignum + 1;
end
figure(fignum);
X=A(:,i)
Y=C(:,i)
R=corrcoef(X,Y);
R_squared=R(2)^2;
R_squared=R_squared*100
R_squared=round(R_squared)
scatter(X,Y);
%Add least-squares line to scatter plot
h=lsline;
set(h,'color','r');
%Use polyfit to compute a linear regression that predicts y from x
p = polyfit(X,Y,1)
p =spa_sf(p,2)
max_x=max(X);
max_y=max(Y);
min_x=min(X)
min_y=min(Y)
mean_x=mean(X)
mean_y=mean(Y)
if (max_x > max_y)
max_value=max_x;
else
max_value=max_y;
end;
if X==A(:,i)
label_x=s(i);
else if X==B(:,i)
label_x=s(i+4);
else if X==C(:,i)
label_x=s(i+8);
else if X==D(:,i)
label_x=s(i+12);
end
if Y==A(:,i)
label_y=s(i);
else if Y==B(:,i)
label_y=s(i+4);
else if Y==C(:,i)
label_y=s(i+8);
else if Y==D(:,i)
label_y=s(i+12);
end
xlabel(label_x); ylabel(label_y);
xlim([0 max_value+max_value/20]);
ylim([0 max_value+max_value/20]);
line([0,max_value+max_value/20],[0,max_value+max_value/20],... 'linewidth',1,... 'color',[0,0,0]);
text(mean_x-mean_x/1.1, max_y-(max_y/4), ['R^2 (%) = ' num2str(R_squared)])
text(mean_x-mean_x/1.1, max_y-(max_y/4)-(max_y/10), ['Y = ' num2str(p(1))])
text(mean_x-mean_x/1.1+max_x/8, max_y-(max_y/4)-(max_y/10), [' X + ' num2str(p(2))])
end
  2 Kommentare
Oleg Komarov
Oleg Komarov am 23 Apr. 2011
It's not warking says nothing. Post the error message or the unwanted result.
Also, format properly the whole code.
Hassan
Hassan am 23 Apr. 2011
it's working fine now, it seems the problem was about 'else if' which should be 'elseif'. thanks Oleg.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Oleg Komarov
Oleg Komarov am 23 Apr. 2011
As an indication give a look at plotmatrix
  2 Kommentare
Hassan
Hassan am 23 Apr. 2011
thanks Oleg for introducing plotmatrix. it's an interesting function. but I don't think it does what I want. for example for A and B array, I want the scatter:
A(:,1) and B(:,1)
A(:,2) and B(:,2)
A(:,2) and B(:,2)
A(:,2) and B(:,2)
which are four plots for two arrays while ploymatrix() produces 16 plots.I want to do the same for AC (A and C),AD,BC,BD and CD. I also want to add a specific label to x axis and y axis of each plots (indicated with S={s1,..,s16}), add regression equation(and line) and r-squared, add a 1:1 line, make the length of x and y axis of each plot as the maximum value of the data.
Hassan
Hassan am 23 Apr. 2011
A(:,1) and B(:,1)
A(:,2) and B(:,2)
A(:,3) and B(:,3)
A(:,4) and B(:,4)

Melden Sie sich an, um zu kommentieren.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by