Effacer les filtres
Effacer les filtres

How to stop legend from adding 'data1', 'data2' when additional data is plotted

234 vues (au cours des 30 derniers jours)
KAE
KAE le 4 Mai 2017
Commenté : DGM le 12 Juil 2023
In 2017a, when I make some plots and add a legend, then add subsequent plots, the legend is updated with new unwanted entries generically labeled 'data1', 'data2', etc. How can I "freeze" the legend so it does not update? I am not able to re-run the legend command after all plots are drawn because the legend is generated within a function, and the additional lines are plotted at the command line. I can't easily regenerate the legend from the command line because the real plot is complicated, but if there is a way to manually edit the legend command and delete the 'data1' and 'data2' lines and labels, that would work.
figure;
h1 = plot(rand(1,7), rand(1,7));
hold on;
h2 = plot(rand(1,7), rand(1,7));
h3 = plot(rand(1,7), rand(1,7));
% We only want legend entries for the first three lines
lh = legend([h1 h2 h3], ...
'First line', 'Second line', 'Third line');
pause; % This is the legend as it should appear
% Additional lines
h4 = plot(rand(1,7), rand(1,7));
h5 = plot(rand(1,7), rand(1,7));
% Undesired legend entries appear for additional lines
% labeled 'data1' and 'data2'
  2 commentaires
ks905383
ks905383 le 4 Mai 2017
Which version of Matlab are you using? Running the code on mine (2014b) gives me what you want - no additional legend entries for the new guys, so it might be a version-specific issue.
KAE
KAE le 4 Mai 2017
Good thought. I added that I am running R2017a.

Connectez-vous pour commenter.

Réponse acceptée

Steven Lord
Steven Lord le 5 Mai 2017
The entry in the Release Notes describing this new behavior includes instructions for four different ways to avoid including an item in the legend.
One way prevents the legend from updating at all.
One way specifies what should be included when the legend is created, but notes that objects created after that point are automatically added.
One way prevents an object from being added to the legend when the object is created.
One way may not be able to prevent the object from being added to the legend, but I believe it can remove the item from the legend almost immediately after it is created. [I haven't tried this last approach, but I don't think you can do it during construction of the object.]
  6 commentaires
Arturo
Arturo le 21 Fév 2023
Set 'AutoUpdate','off' worked for me
DGM
DGM le 12 Juil 2023
That information has since been removed from the online release notes.
This is copypasted from the PDF release notes:
legend Function: Create legends that update when data is added to or removed from the axes
Legends now automatically update when you add or remove graphics objects from the axes. Previously, legends did not automatically add items for new graphics objects or remove items for deleted graphics objects.
For example, this code plots a line and adds a legend. Then the code plots a second line. The legend automatically updates to include the second line.
plot(1:10)
legend('Line 1')
hold on
plot(11:20,'DisplayName','Line 2')
hold off
Compatibility Considerations
If you add or delete a graphics object after creating a legend, the number of items in the legend can differ from previous releases. For example, plot a line and create a legend. The plot displays a marker at the maximum point.
x = linspace(0,2*pi);
y = sin(x);
plot(x,y)
legend('sin(x)')
hold on
plot(pi/2,sin(pi/2),'o','MarkerSize',12)
hold off
If you do not want the legend to update automatically, set the AutoUpdate property of the legend to 'off'. Graphics objects added to the axes after the legend is created do not appear in the legend.
legend('sin(x)','AutoUpdate','off')
To change the behavior for newly created legends, set the default value of the AutoUpdate property. To affect all new legends, set the value on the root level. Alternatively, to affect all new legends in a particular figure, set the value on the figure level. After you set the default value, any new legends have an AutoUpdate property set to 'off'.
fig = figure;
set(fig,'defaultLegendAutoUpdate','off')
Additional options for excluding specific graphics objects from a legend include:
• Specifying the graphics objects to include in the legend as an input argument to the legend function. For example, this code creates a legend that includes only the graphics objects referred to by p1 and p2. However, graphics objects added to the axes after the legend is created do appear in the legend. Consider creating the legend after creating all the plots to avoid extra items.
p1 = plot(1:10);
hold on
p2 = plot(11:20);
p3 = plot(21:30);
legend([p1 p2],'line 1','line 2')
• Excluding graphics objects from the legend by setting their HandleVisibility property to 'off'. The HandleVisibility property also controls the visibility of the graphics object in the Children property of its parent.
p4 = plot(31:40,'HandleVisibility','off');
• Excluding graphics objects from the legend by setting the IconDisplayStyle property of the underlying annotation object to 'off'.
p5 = plot(41:50);
p5.Annotation.LegendInformation.IconDisplayStyle = 'off';

Connectez-vous pour commenter.

Plus de réponses (1)

KAE
KAE le 4 Mai 2017
Modifié(e) : KAE le 4 Mai 2017
It turns out that the undesired entries can be deleted using the 'String' property of the legend. Below lh is the legend handle obtained by clicking on the legend then running the following at the command line,
lh = gco;
Then the string property of the legend is modified as follows,
s = get(lh, 'string');
set(lh, 'string', s(1:4));
  2 commentaires
Hassan Sultan
Hassan Sultan le 1 Jan 2019
How can I delete number 2 of s(1:4) as an example, please.
KAE
KAE le 2 Jan 2019
Modifié(e) : KAE le 7 Jan 2019
It depends what you want. If you want the 2nd line to appear in the legend with no text label, click on the legend then,
lh = gco;
s = get(lh, 'string');
s{2} = '' ; % Remove the text labeleing the 2nd line
set(lh, 'string', s(1:4)); % But the line still appears in the legend
If you don't want the 2nd line to appear in the legend at all (neither the line nor the text label), then do the following,
figure;
h1 = plot(rand(1,7), rand(1,7));
hold on;
h2 = plot(rand(1,7), rand(1,7));
h3 = plot(rand(1,7), rand(1,7));
h4 = plot(rand(1,7), rand(1,7));
h5 = plot(rand(1,7), rand(1,7));
% Don't include the handle h2 to the 2nd line in the legend call
lh = legend([h1 h3 h4 h5], ...
'First line', 'Third line', 'Third line', 'Fourth line', 'Fifth Line');

Connectez-vous pour commenter.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by