How do I skip items in a legend?

조회 수: 9,233(최근 30일)
henry wang
henry wang 2011년 1월 25일
편집: Matt Lobo 2021년 11월 30일 0:05
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.
  댓글 수: 13
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Kenneth Eaton
Kenneth Eaton 2011년 1월 25일
편집: MathWorks Support Team 2021년 3월 16일
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.
  댓글 수: 5
Tomer Nahshon
Tomer Nahshon 2021년 3월 24일
@Zheng Liu Wonderful answer,
Thank you!

댓글을 달려면 로그인하십시오.

추가 답변(11개)

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.
  댓글 수: 5
Fernando Zigunov
Fernando Zigunov 2021년 11월 29일 19:22
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.
  댓글 수: 4
Brent F
Brent F 2021년 6월 22일
@Jim Tonti Yes! Upvote his solution: legend({'A','B'},'AutoUpdate','off')

댓글을 달려면 로그인하십시오.


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');
  댓글 수: 3
Carl Witthoft
Carl Witthoft 2021년 9월 8일
doesn't seem to work for a "fill" object

댓글을 달려면 로그인하십시오.


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
  댓글 수: 1
Riaan Ferreira
Riaan Ferreira 2021년 8월 10일
This worked briliantly

댓글을 달려면 로그인하십시오.


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

댓글을 달려면 로그인하십시오.


Matt Lobo
Matt Lobo 2021년 11월 1일 22:34
편집: Matt Lobo 2021년 11월 30일 0:05
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.
  댓글 수: 1
Giuseppe Degan Di Dieco
Giuseppe Degan Di Dieco 2021년 11월 2일 8:49
Thank you Matt for updating the thread.
Keep in touch for tips sharing and creating a MATLAB users community.
Best.

댓글을 달려면 로그인하십시오.


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
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
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
Marya Sadki
Marya Sadki 2021년 11월 29일 12:03
Me too thanks

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by