MATLAB Answers

23

How do I skip items in a legend?

Asked by henry wang on 25 Jan 2011
Latest activity Commented on by TheLast One on 1 Jun 2019
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

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.
Or programmatically:
plot(...,'HandleVisibility','off')
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.

7 Answers

Answer by Kenneth Eaton on 25 Jan 2011
Edited by MathWorks Support Team on 8 Nov 2018
 Accepted Answer

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')

  3 Comments

If the lines were created in a seperate file and you can't reach them :
h = findobj(gca,'Type','line');
will get you all of the line objects :)
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.
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!

Sign in to comment.


Answer by Walter Roberson
on 25 Jan 2011
Edited by 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.

  3 Comments

this is a much nicer solution. kudos
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.
This is the ideal solution for implementing "plot" in a loop where it is difficult to assign a unique handle each iteration. Thank you!

Sign in to comment.


Answer by 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.


Answer by Dilshad Raihan on 26 Oct 2015
Edited by 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

Thnak you sir, for giving the solution

Sign in to comment.


Answer by Junette Hsin on 21 Mar 2019
Edited by 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.

  1 Comment

Simplest solution :)

Sign in to comment.


Answer by Akshay Ravindran on 26 Nov 2015

Why is it that this error keeps coming up?
<<
>>

  3 Comments

[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?
If z,x,c etc are the handles, then remove the commas:
i.e. [z w c ...] and you are good to go!
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.


Answer by 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

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.
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.