Keeping previous legend in multiple plots done by a function
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello everybody,
I have a function, say my_plot_function , which plots simultion results. If I run my_plot_function more times, I can plot the different data on the same figure, however the legend is not kept and only the legend for the last my_plot_function run is displayed.
I want to hold not only the previous plots, but also the previous legend, and the plots are done by launching my_plot_function .
How can I keep also the previous legends, in addition to the plot?
Thanks
Patrizio
0 commentaires
Réponses (1)
Monisha Nalluru
le 8 Mar 2021
Modifié(e) : Monisha Nalluru
le 8 Mar 2021
You can use hold method to retain the current plot while adding new plot. Legend method to add legends to axes
Here is an example of retaining legend on the axes while calling function to plot new data
function my_plot(x,y,legendName)
plot(x,y,'DisplayName',legendName);
legend
end
x=linspace(1,10);
y=x*2;
y1=x*3;
y2=x*4;
my_plot(x,y,'x*2');
hold on
my_plot(x,y1,'x*3');
my_plot(x,y2,'x*4');
hold off
Hope this helps!
2 commentaires
Voir également
Catégories
En savoir plus sur Legend dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!