Locking the Legend even if the object is deleted
8 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Ruben Kopischke
il 7 Feb 2023
Commentato: Ruben Kopischke
il 8 Feb 2023
Hello,
I want the AutoUpdate of my legend to be "off" no matter whether I delete an object from the axes, but:
(since R2022b): If you delete an object from the axes, the legend updates to reflect the change regardless of whether this property (AutoUpdate) is set to "on" or "off".
I have programmed a simulation where the legend of the axes should stay as it is, no matter which objects you delete in the plot. Do you have any idea how I can implement this in newer versions of Matlab?
Warm regards,
Ruben
0 Commenti
Risposta accettata
Benjamin Kraus
il 7 Feb 2023
Modificato: Benjamin Kraus
il 7 Feb 2023
As @Bjorn Gustavsson suggests, your best bet is to create proxy objects that are represented in the legend, but are not visible in the axes. @Bjorn Gustavsson says to place them "outside the visible area", but my recommendation is to use NaN data so they are not visible. For example:
% Objects with real data.
p = plot(magic(5));
% Create proxy objects with NaN data, which will now be rendered, but can
% be put into the legend.
hold on
pLegend = plot(NaN(5)); % Proxy objects with NaN data.
hold off
% Copy the SeriesIndex from the objects with real data to the proxy
% objects, which will make the colors and line styles match.
set(pLegend, {'SeriesIndex'},{p.SeriesIndex}');
% Use the proxy objects in the legend.
legend(pLegend);
% Now you can delete the real objects, but the proxy object will remain in the legend.
delete(p(3))
5 Commenti
Più risposte (1)
Bjorn Gustavsson
il 7 Feb 2023
If you explicitly set the axis of the plot, then you can hide objects outside the visible are and use their handles for the legend. That way it doesn't matter what objects are still "alive" in your active plot. This is a fairly cludgy way to get around this, but I've found it useful to explain the meaning of different line-widths, line-styles when they display different properties across all lines.
Vedere anche
Categorie
Scopri di più su Data Distribution Plots in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!