How to add multiple legends in a loop for mulitple plots?

2 visualizaciones (últimos 30 días)
Hi there,
I have, say, 3 plots generated in a loop like the following:
for r=1:3
figure;
px= vardata(:,250, r);
plot(px,'-', 'color',rand(1,3));
end
Now, I need to add legends to each plot as pixel 1, pixel 2, pixel 3 etc automaticlally.
I am used to add legends manually and have not worked on adding legends in a loop before.
Many Thanks

Respuesta aceptada

Walter Roberson
Walter Roberson el 13 de Nov. de 2022
for r=1:3
figure;
px= vardata(:,250, r);
plot(px,'-', 'color',rand(1,3), 'DisplayName', "pixel " + r);
legend show
end
However since you are only displaying one line per figure window, normally you would use a title() instead of legend()
  1 comentario
Naif Alsalem
Naif Alsalem el 13 de Nov. de 2022
Many thanks @Walter Roberson, for the answer and suggestion. Yes title for one-line plot is really helpful.
Thanks again.
Cheers,

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Legend en Help Center y File Exchange.

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by