How do I skip items in a legend?

4,784 ビュー (過去 30 日間)
henry wang
henry wang 2011 年 1 月 25 日
回答済み: Image Analyst 2024 年 9 月 25 日
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.
  14 件のコメント
Greg Vieira
Greg Vieira 2021 年 9 月 4 日
How can you do this for multiple plots? For example, I have 1001 data sets plotted and only want the legend to show 1 of the first 1000 plots and the last plot. It is not reasonable for me to place 999 ' ' placeholders.
Kaveh Vejdani
Kaveh Vejdani 2023 年 6 月 10 日
First plot data1, then data1001, the set L.AutoUpdate = 'off'; % L=Legend

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

採用された回答

Kenneth Eaton
Kenneth Eaton 2023 年 8 月 29 日
編集済み: MathWorks Support Team 2022 年 1 月 10 日
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.
plot(rand(3)); 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.
For previous releases, 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') 
  10 件のコメント
DGM
DGM 2022 年 10 月 18 日
編集済み: DGM 2022 年 10 月 18 日
I was responding to @Dan Houck who was asking specifically for a means to avoid the implicit item removal that happens in R2021a. Leaving the unlabeled marker was the goal.
As to whether what Dan asked for is visually objectionable, you're free to tell him.
Kaveh Vejdani
Kaveh Vejdani 2023 年 6 月 10 日
L = legend;
L.AutoUpdate = 'off';

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

その他の回答 (13 件)

Matt Lobo
Matt Lobo 2021 年 11 月 1 日
編集済み: Matt Lobo 2021 年 11 月 30 日
Essentially set the 'HandleVisibility' attribute to 'off' when plotting something, as such:
plot(x,y,'HandleVisibility','off')
This has some implications concerning interacting with that handle in other ways, but if you don't plan on using the handle, this is a great dynamic way to not include certain plots in your legend. It works especially well when you're plotting iteratively, and don't want to store handles and then hard-code the legend to fit your exact plot.
  5 件のコメント
Andres Ricardo Herrera Orozco
Andres Ricardo Herrera Orozco 2024 年 1 月 16 日
Really helpful. Thanks!
Hannah Tang
Hannah Tang 2024 年 9 月 25 日
Fantastic advice. Thank you.

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


Walter Roberson
Walter Roberson 2011 年 1 月 25 日
編集済み: Rik 2022 年 4 月 5 日
You can set the IconDisplayStyle to off for the items you do not wish a legend for. See this documentation.
Edit by @Rik (2022/04/05):
The link above was valid for the documentation from R2012a. The equivalent page in R2022a suggests a different strategy (i.e. only providing the handles to legend for the objects you wish to include). In the current release IconDisplayStyle is documentated under the properties of the graphics primitives (e.g. line objects or patch objects).
Documentation pages from specific releases will remain online for 5 years.
  6 件のコメント
Fernando Zigunov
Fernando Zigunov 2021 年 11 月 29 日
broken link!
Tanya Meyer
Tanya Meyer 2022 年 4 月 5 日
Yes - broken link :(

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


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.
  5 件のコメント
Brent F
Brent F 2021 年 6 月 22 日
編集済み: Brent F 2021 年 8 月 10 日
@Jim Tonti Yes! Upvote his solution: legend({'A','B'},'AutoUpdate','off')
Gabriela Belicova
Gabriela Belicova 2022 年 5 月 13 日
Both very helpful, thank you so much !!!

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


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');
  4 件のコメント
Carl Witthoft
Carl Witthoft 2021 年 9 月 8 日
doesn't seem to work for a "fill" object
Soham Sinha
Soham Sinha 2022 年 5 月 11 日
Thanks

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


Boris Blagojevic
Boris Blagojevic 2021 年 6 月 23 日
An alternative approach: Prevent the legend from updating
First, plot the lines that you want to have labeled. Then, specify the legend and set
legend(....,'AutoUpdate','off')
then, plot the remaining lines
  4 件のコメント
Rik
Rik 2022 年 6 月 13 日
Comment posted as flag by Fatima u'wais:
intersted
Dr.Jaber Aljuaidiyah
Dr.Jaber Aljuaidiyah 2024 年 8 月 20 日
This works nicely. Thanks pal

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


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.

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

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


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
  1 件のコメント
Amir Semnani
Amir Semnani 2021 年 6 月 9 日
Thanks. That worked for me (MATLAB 2017b) and it's very simple. Let's assume we have 8 datasets and we want to plot all of them, but only want to see the legend for dataset with even number.
x=ones(100,1)*(1:8); plot(x);ylim([0 9]); hleg = legend ('show'); hleg.String(1:2:end)=[];

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


Akshay Ravindran
Akshay Ravindran 2015 年 11 月 26 日
Why is it that this error keeps coming up?
<<
>>
  3 件のコメント
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');
  4 件のコメント
Brent F
Brent F 2021 年 8 月 10 日
Have you gotten this method of generating a legend using plot handles to work within a subplot?
Rik
Rik 2021 年 8 月 11 日
@Brent F A subplot is simply a new axes object, so any method should work. You should be careful when using gca or when not supplying a handle at all, as the last axes with user interaction will be the target of your calls.

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


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')
  2 件のコメント
Bart Boonstra
Bart Boonstra 2021 年 4 月 12 日
Thanks that worked for me!
Marya Sadki
Marya Sadki 2021 年 11 月 29 日
Me too thanks

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


K Anderson
K Anderson 2024 年 8 月 30 日
If you plot multiple lines with the same plot command like this
h(1,:) = plot(rand(4,11),'r')
h =
1x11 Line array: Line Line Line Line Line Line Line Line Line Line Line
hold on
h(2,:) = plot(rand(4,11),'b')
h =
2x11 Line array: Line Line Line Line Line Line Line Line Line Line Line Line Line Line Line Line Line Line Line Line Line Line
and use
legend
on this, you get a 2x11 long legend
but you only want to highlight the first red and first blue line use this;
legend([h(1,1) h(2,1)],'red','blue')
This is mentioned in the documentation as;
legend(subset,___) only includes items in the legend for the data series listed in subset. Specify subset as a vector of graphics objects. You can specify subset before specifying the labels or with no other input arguments.

Image Analyst
Image Analyst 2024 年 9 月 25 日
Every time you plot, get the handle to the points or curves. Then pass only those that you want into legend. Here is a full demo:
lineSize = 2;
markerSize = 30;
% Define 3 data sets
x1 = [-1.9, 0.004, 1.94];
x2 = [-1.94, -0.23, 1.95];
x3 = [-1.92, .2, 1.93];
y1 = [13.885, 10.168, 14.235];
y2 = [9.3805, 5.2697, 9.3367];
y3 = [4.5262, 0.80904, 4.0889];
% Interpolate the 3 curves.
xFit = linspace(-2, 2, 500);
coefficients1 = polyfit(x1, y1, 2);
yFit1 = polyval(coefficients1, xFit);
coefficients2 = polyfit(x2, y2, 2);
yFit2 = polyval(coefficients2, xFit);
coefficients3 = polyfit(x3, y3, 2);
yFit3 = polyval(coefficients3, xFit);
% Plot the 3 data sets and their interpolated curves
handle1 = plot(x1, y1, 'r.', 'MarkerSize', markerSize);
hold on;
handle2 = plot(xFit, yFit1, 'r-', 'LineWidth', lineSize);
handle3 = plot(x2, y2, 'b.', 'MarkerSize', markerSize);
handle4 = plot(xFit, yFit2, 'b-', 'LineWidth', lineSize);
handle5 = plot(x3, y3, 'g.', 'MarkerSize', markerSize);
handle6 = plot(xFit, yFit3, 'g-', 'LineWidth', lineSize);
grid on;
% Have the legend only for the data, not the interpolated fit
% by passing in only the plot handles of the data.
legend([handle1, handle3, handle5], 'Data1', 'Data2', 'Data3', 'Location', 'north');

カテゴリ

Help Center および File ExchangeLegend についてさらに検索

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!

Translated by