How do I fix my legend?

조회 수: 12 (최근 30일)
Connor Miller
Connor Miller 2020년 10월 27일
댓글: Star Strider 2020년 10월 28일
Hello, I am creating a graph with mutiple for loops to obtain data, the problem at the moment is when I try to creat a legend corresponding to each specific value the it is only seeing the first for loop.
figure, hold on
for i=1:size(AB,1)
a1 = VAB(:,1)'*AB(i,:)';
b1 = VAB(:,2)'*AB(i,:)';
plot(a1,b1,'ko','LineWidth',3)
end
for i=1:size(CD,1)
c1 = VCD(:,1)'*CD(i,:)';
d1 = VCD(:,2)'*CD(i,:)';
plot(c1,d1,'bo','LineWidth',3)
end
for i=1:size(EF,1)
e1 = VEF(:,1)'*EF(i,:)';
f1 = VEF(:,2)'*EF(i,:)';
plot(e1,f1,'go','LineWidth',3)
end
for i=1:size(GH,1)
g1 = VGH(:,1)'*GH(i,:)';
h1 = VGH(:,2)'*GH(i,:)';
plot(g1,h1,'mo','LineWidth',3)
end
xlabel('PC1'),ylabel('PC2')
grid on, set(gca,'FontSize',15)
axis ([-1 1 -.1 .1])
legend('CAP','Capsiate','RTX','CPZ')

채택된 답변

Star Strider
Star Strider 2020년 10월 27일
For every plot call, return a handle:
h1 = plot(a1,b1,'ko','LineWidth',3);
. . .
h2 = plot(c1,d1,'bo','LineWidth',3);
. . .
h3 = plot(e1,f1,'go','LineWidth',3);
. . .
h4 = plot(g1,h1,'mo','LineWidth',3);
then in the legend call:
legend([h1 h2 h3 h4],'CAP','Capsiate','RTX','CPZ')
That should do what you want.
I cannot test it with your code, so I am posting this as UNTESTED CODE. It should work.
  댓글 수: 2
Connor Miller
Connor Miller 2020년 10월 27일
That worked perfectly, thank you so much!!!
Star Strider
Star Strider 2020년 10월 28일
As always, my pleasure!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by