MATLAB Answers

How do I skip items in a legend?

10.095 views (last 30 days)
henry wang
henry wang on 25 Jan 2011
Edited: Diaa on 17 Nov 2020 at 15:21
How do I skip items in legend? Say I have 6 plots with 3 actual values and 3 interpolated curves. I only want to label the actual value curves so
legend('first','','second','','third')
doesn't really work because the interpolated curve still shows.

  3 Comments

EhsanZ
EhsanZ on 12 Mar 2018
1- Select the curve you don't want have legend. 2- Go to the "more properties" (while the curve is still selected). 3- Turn "HandleVisibility" off.
FTil
FTil on 16 Jun 2018
Or programmatically:
plot(...,'HandleVisibility','off')
Akash Menon
Akash Menon on 13 Dec 2018
figure
h1=histfit(no_ess,20,'gamma');
xlim([0 1])
hold on
h2=histfit(with_ess,50,'kernel');
xlim([0 1])
set(get(get(h1(2),'Annotation'),'LegendInformation'),'IconDisplayStyle','off');
set(get(get(h2(2),'Annotation'),'LegendInformation'),'IconDisplayStyle','off');
legend('MCR SOL','MCR SOL ESS')
Here's a quick sample of how it worked- for my histograms I didn't want the two red lines to be labelled in the legend. I found out that for the histograms- it creates a handle h(1) and (2) where 1 corresponds to the bar charts and 2 to the fitting lines.
Just an example- hope it helps!
sample.PNG

Sign in to comment.

Accepted Answer

Kenneth Eaton
Kenneth Eaton on 25 Jan 2011
Edited: MathWorks Support Team on 8 Nov 2018
To exclude items from a legend, you can specify the objects that you want to include as the first input argument to the “legend” function.
For example, plot three lines and return the “Line” objects as array “p”. Include only the first and third lines in the legend by specifying “p(1)” and “p(3)” as the first input argument to “legend”.
p = plot(rand(3));
legend([p(1) p(3)],'plot 1','plot 3')
Alternatively, you can set the “IconDisplayStyle” to “off” for the object that you do not want to include in the legend. For example, exclude the second “Line” object, “p(2)”.
p = plot(rand(3));
set(get(get(p(2),'Annotation'),'LegendInformation'),'IconDisplayStyle','off');
legend('plot 1','plot 3')

  4 Comments

Show 1 older comment
Walter Roberson
Walter Roberson on 17 Jan 2017
Dan, in terms of the original Question, that would get you the handles of the "actual" lines together with the "interpolated" lines. Potentially you might know which is which by indexing; otherwise you might need to examine the line properties such as the linestyle in order to figure out whether it is one of the ones you want or not.
Zheng Liu
Zheng Liu on 10 Jan 2018
Hi Ken, I found the solution works most cases, except I have an unexist line (which has an empty handle). I want to skip the legend of the line, if the line not there. For example:
h1 = plot([]); % Blue line
hold on;
h2 = plot(rand(1,10),'r'); % Red line
h3 = plot(rand(1,10),'g'); % Green line
legend([h1 h2 h3],{'want to skip this if line unexist', 'hello', 'world'});
However, the legend for h1 will be assigned on h2, and the legend for h3 will be ignored. Do you have any idea of this? Thanks!
Jeff Miller
Jeff Miller on 21 May 2020
For anyone using this technique, be advised it seems that legend('Location','SouthEast') resets the plot to give the full set of legends, even those dropped (at least in 2016b). For example,
legend([h1 h2],{'Leg1' 'Leg2'});
legend('Location','SouthEast'); % this sequence shows all legends, not just 1st two.
legend('Location','SouthEast'); % this sequence shows just the first 2 legends
legend([h1 h2],{'Leg1' 'Leg2'});
Not sure if this is a bug or a feature. :)

Sign in to comment.

More Answers (9)

Walter Roberson
Walter Roberson on 25 Jan 2011
Edited: John Kelly on 26 Feb 2015
You can set the IconDisplayStyle to off for the items you do not wish a legend for. See this documentation.

  4 Comments

Show 1 older comment
Danny Smith
Danny Smith on 27 Apr 2015
Wow, this solution is much smoother than the other proposed, and more generally applicable.
It allows you to turn off the ones you don't want, rather than having to turn on the ones you do. If you (like me) are plotting many data sets on the same graph, this is much more useful and simple.
If you have the handle for the data set you DON'T want included, all you need is this line:
set(get(get(h,'Annotation'),'LegendInformation'),'IconDisplayStyle','off');
where "h" is changed to the name of your handle.
For more information, follow the link provided by Walter and John.
Tom
Tom on 22 Apr 2016
This is the ideal solution for implementing "plot" in a loop where it is difficult to assign a unique handle each iteration. Thank you!
Nuwan Liyanage
Nuwan Liyanage on 25 Aug 2020
This is really helpful, thanks!

Sign in to comment.


the cyclist
the cyclist on 25 Jan 2011
Each curve has a handle, which can be obtained from the properties. Use the form of legend that takes two arguments (handle and legend), and only use the handles of those curves that you want to show.

  0 Comments

Sign in to comment.


Junette Hsin
Junette Hsin on 21 Mar 2019
Edited: Junette Hsin on 21 Mar 2019
I ran into this problem and I have not seen this method suggested yet, but I solved it by changing the order of my plotted lines which affects what the legend displays (I am using MATLAB R2017b).
Let's say you plot 2 lines first, and then create a legend. Then you plot a 3rd line. That 3rd line will be added to your legend as 'data 1'.
Instead plot 3 lines, and then in your legend, label just the first 2 lines. The 3rd line will be omitted from the legend.
Hope this helps.

  2 Comments

TheLast One
TheLast One on 1 Jun 2019
Simplest solution :)

Sign in to comment.


Yasin Zamani
Yasin Zamani on 25 Sep 2019
Edited: Yasin Zamani on 25 Sep 2019
For example, suppose you want to skip the name of the first plot in the legend:
x = linspace(0, 2 * pi);
% sin(x)
h = plot(x, sin(x));
% the following line skip the name of the previous plot from the legend
h.Annotation.LegendInformation.IconDisplayStyle = 'off';
% cos(x)
plot(x, cos(x));
% legend
legend('cos');

  1 Comment

Giulio Suzumura
Giulio Suzumura on 17 Oct 2020
Tks. Best approach when uses 'hold on's and external functions.

Sign in to comment.


Dilshad Raihan
Dilshad Raihan on 26 Oct 2015
Edited: Dilshad Raihan on 26 Oct 2015
You can do this by first plotting the curves in an order so that the lines you don't want to be displayed in the legend comes in the end. That is, suppose you have N lines to be plotted but you dont want to display m of these in the legend. Then first plot the required N-m lines and then the remaining m. After that, turn the legend on, click on the legend and the "legend property editor" will be displayed. Go to the "more properties" option. You can see an entry titled "String" specified as a "1xN cell array". Click on the cell array icon and set the size as "1xN-m". Now, only the first N-m curves will be displayed in Legend.

  1 Comment

Harish Pulluri
Harish Pulluri on 26 Sep 2016
Thnak you sir, for giving the solution

Sign in to comment.


Akshay Ravindran
Akshay Ravindran on 26 Nov 2015
Why is it that this error keeps coming up?
<<
>>

  3 Comments

Walter Roberson
Walter Roberson on 26 Nov 2015
[z, x, c, v, b, n, m] looks like it might be intended as the list of "entries" to be annotated. Is it possible that those variables all hold column vectors instead of scalars, with the result that [z, x, c, v, b, n, m] is a 2 dimensional array?
Ajith Tom George
Ajith Tom George on 17 Nov 2016
If z,x,c etc are the handles, then remove the commas:
i.e. [z w c ...] and you are good to go!
Walter Roberson
Walter Roberson on 17 Nov 2016
No, in each case where z w c etc are expressions that have no spaces in them, [z w c ...] is the same as [z, w, c, ...]
Spaces in expressions sometimes trigger parsing as if there were multiple expressions. For example:
[1 -2*x]
is considered two expressions, 1 and -2*x

Sign in to comment.


Luke Marsden
Luke Marsden on 2 Feb 2017
I am trying to do a similar thing using this line of code:
leg = legend([p4 RETU_Average activity1 Vulcanian1], 'Tilt', 'RETU Mean Amplitude', 'Activity', '"Vulcanian" Explosions', 'Location', 'northeast');
I am getting this error:
Error using matlab.graphics.chart.primitive.Line/horzcat
Cannot convert double value 23 to a handle
Error in p1_zoom_plot (line 93)
leg = legend([p4 RETU_Average, activity1 Vulcanian1], 'Tilt', 'RETU Mean Amplitude', 'Activity', '"Vulcanian" Explosions', 'Location', 'northeast');

  2 Comments

Walter Roberson
Walter Roberson on 2 Feb 2017
One of p4 RETU_Average activity1 Vulcanian1 contains the numeric value 23 instead of containing the handle to a graphics object.
The comma looks suspicious there.
I speculate that you might have passed in the values you are plotting rather than a copy of the handle that you got when you plotted them.
Luke Marsden
Luke Marsden on 3 Feb 2017
Thanks for your reply Walter. With your help I have solved the problem.
23 is the first value in the vector named 'RETU_Average'. I was trying to pass the vector into the legend rather than the handle, which I created using this line of code.
p1 = plot (time, RETU_Average, 'LineWidth', 2);
New legend text for reference of anyone else who has made a similar mistake:
leg = legend([p4 p1 activity1 Vulcanian1], 'Tilt', 'RETU Mean Amplitude', 'Activity', '"Vulcanian" Explosions', 'Location', 'northeast');

Sign in to comment.


Juan Carlos de Luna
Juan Carlos de Luna on 6 Apr 2020
Select line in Plot Browser and type
set(get(get(gco,'Annotation'),'LegendInformation'),'IconDisplayStyle','off')

  0 Comments

Sign in to comment.


Diaa
Diaa on 17 Nov 2020 at 15:19
Edited: Diaa on 17 Nov 2020 at 15:21
You can simply delete the last undesired entry by the following:
% assume you plotted some curves before this line and all of them are desired to be shown in the legend
hleg = legend('show');
plot(x,y) % you don't need this plot in the legend
hleg.String(end) = []; % delete the last legend entry of the very last plot
% continue plotting while copy and paste the previous line immediately after any plot you don't need in the legend

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by