How can i add legend in a for loop after each plot ?
Afficher commentaires plus anciens
In below code legend got updated each times thereby leaving me with only the last legend but i want is like 1st iteration plot and its legend and next plot and its legends ;thereby all legends should be there.
for i=2:2:20
y=(P(:,1));
plot((P(:,i+1)),y,'color',rand(1,3),'marker','s')
h{i}=char(headings(i));
legend(h(i))
end
Réponse acceptée
Plus de réponses (1)
I just googled this question and found this topic... It has beena problem for me for a long time and now i founded a solution...
...reading the help of "legend" and assigning the output of the function to a variable you can be able to modify "live" the legend created in automatic, as far as you first plot and the modify it.
this is because once you enable the legend() on a plot, with the AutoUpdate option turned on, Matlab will automatically create a new legend once you plot a new line.
See the following example:
x = linspace(0,pi);
figure
my_legend = legend();
hold on, grid on
for i=1:5
plot(x,cos(i*x))
my_legend.String{i} = ['cos(',num2str(i),'x)'];
end
Catégories
En savoir plus sur Legend dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
