How do I fix my legend?

14 visualizaciones (últimos 30 días)
Connor Miller
Connor Miller el 27 de Oct. de 2020
Comentada: Star Strider el 28 de Oct. de 2020
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')

Respuesta aceptada

Star Strider
Star Strider el 27 de Oct. de 2020
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 comentarios
Connor Miller
Connor Miller el 27 de Oct. de 2020
That worked perfectly, thank you so much!!!
Star Strider
Star Strider el 28 de Oct. de 2020
As always, my pleasure!

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by