MATLAB Answers

How do I skip items in a legend?

10,213 views (last 30 days)
henry wang
henry wang on 25 Jan 2011
Commented: Daniel Paredes on 6 Apr 2021 at 12:55
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
doesn't really work because the interpolated curve still shows.
Daniel Paredes
Daniel Paredes on 6 Apr 2021 at 12:55
@FTil I was looking exactly for this, thank you very much! Simplest solution of everything that's been posted here.

Sign in to comment.

Accepted Answer

Kenneth Eaton
Kenneth Eaton on 25 Jan 2011
Edited: MathWorks Support Team on 16 Mar 2021 at 18:50
Starting in R2021a, you can leave an item out of a legend by setting the corresponding label to an empty character vector.
For example, plot three lines. Then call the legend function and specify the second legend label as an empty character vector. The corresponding line is omitted from the legend.
legend('Line 1','','Line 3')
Note that this strategy works when you specify just the labels, and not when you specify a set of objects to include in the legend.
Tomer Nahshon
Tomer Nahshon on 24 Mar 2021 at 14:20
@Zheng Liu Wonderful answer,
Thank you!

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.
Nuwan Liyanage
Nuwan Liyanage on 25 Aug 2020
This is really helpful, thanks!

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.

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.

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

Diaa on 17 Nov 2020
Edited: Diaa on 17 Nov 2020
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

Community Treasure Hunt

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

Start Hunting!