MATLAB Answers

How do I skip items in a legend?

7,216 ビュー (過去 30 日間)
henry wang
henry wang 2011 年 1 月 25 日
編集済み: Diaa 2020 年 11 月 17 日
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 件のコメント

EhsanZ
EhsanZ 2018 年 3 月 12 日
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 2018 年 6 月 16 日
Or programmatically:
plot(...,'HandleVisibility','off')
Akash Menon
Akash Menon 2018 年 12 月 13 日
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

サインインしてコメントする。

採用された回答

Kenneth Eaton
Kenneth Eaton 2011 年 1 月 25 日
編集済み: MathWorks Support Team 2018 年 11 月 8 日
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 件のコメント

表示 1 件の古いコメント
Walter Roberson
Walter Roberson 2017 年 1 月 17 日
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 2018 年 1 月 10 日
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 2020 年 5 月 21 日
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. :)

サインインしてコメントする。

その他の回答 (9 件)

Walter Roberson
Walter Roberson 2011 年 1 月 25 日
編集済み: John Kelly 2015 年 2 月 26 日
You can set the IconDisplayStyle to off for the items you do not wish a legend for. See this documentation.

  4 件のコメント

表示 1 件の古いコメント
Danny Smith
Danny Smith 2015 年 4 月 27 日
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 2016 年 4 月 22 日
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 2020 年 8 月 25 日
This is really helpful, thanks!

サインインしてコメントする。


the cyclist
the cyclist 2011 年 1 月 25 日
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 件のコメント

サインインしてコメントする。


Junette Hsin
Junette Hsin 2019 年 3 月 21 日
編集済み: Junette Hsin 2019 年 3 月 21 日
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 件のコメント

TheLast One
TheLast One 2019 年 6 月 1 日
Simplest solution :)
Orhan Soyuhos
Orhan Soyuhos 2020 年 6 月 13 日
Thank you!

サインインしてコメントする。


Yasin Zamani
Yasin Zamani 2019 年 9 月 25 日
編集済み: Yasin Zamani 2019 年 9 月 25 日
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 件のコメント

Giulio Suzumura
Giulio Suzumura 2020 年 10 月 17 日
Tks. Best approach when uses 'hold on's and external functions.

サインインしてコメントする。


Dilshad Raihan
Dilshad Raihan 2015 年 10 月 26 日
編集済み: Dilshad Raihan 2015 年 10 月 26 日
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 件のコメント

Harish Pulluri
Harish Pulluri 2016 年 9 月 26 日
Thnak you sir, for giving the solution

サインインしてコメントする。


Akshay Ravindran
Akshay Ravindran 2015 年 11 月 26 日
Why is it that this error keeps coming up?
<<
>>

  3 件のコメント

Walter Roberson
Walter Roberson 2015 年 11 月 26 日
[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 2016 年 11 月 17 日
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 2016 年 11 月 17 日
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

サインインしてコメントする。


Luke Marsden
Luke Marsden 2017 年 2 月 2 日
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 件のコメント

Walter Roberson
Walter Roberson 2017 年 2 月 2 日
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 2017 年 2 月 3 日
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');

サインインしてコメントする。


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

  0 件のコメント

サインインしてコメントする。


Diaa
Diaa 2020 年 11 月 17 日
編集済み: Diaa 2020 年 11 月 17 日
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 件のコメント

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by