Tweak legend entries without creating a new legend
63 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Chad Greene
le 11 Juil 2014
Réponse apportée : Rodrigo Bernal
le 13 Juil 2021
I have created a plot with a legend and now I would like to edit legend entries without recreating the whole legend. For example, for five legend entries I would like to reorder them like
newOrder = [2 3 5 4 1];
I can reorder the entries by first getting current legend handles and strings like this:
[~,~,plot_h,text_strings] = legend;
And then I can reorder the entries like this:
legend(plot_h(newOrder),text_strings{newOrder})
However, this doesn't only reorder the legend; it seems to delete the legend I'd already created, with its perfect placement and color scheme, etc, and creates a new legend with entries in the correct new order. How can I edit these entries without screwing up the placement, colors, boxoff, etc that I've already set?
0 commentaires
Réponse acceptée
Plus de réponses (2)
Rodrigo Bernal
le 13 Juil 2021
Edit a specific legend entry of an existing legend:
figu=gca;
figu.Legend.String(6)={'Edit the 6th legend'};
0 commentaires
Ben11
le 11 Juil 2014
Modifié(e) : Ben11
le 11 Juil 2014
Maybe assign a handle to your legend in the first place, eg
hLegend = legend(...);
and then change the String property with the new order:
set(hLegend,'String',newOrder);
This should only update the current legend without replacing it.
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!