437 views (last 30 days)
Show older comments
Aravin on 5 Jan 2012
-
-
Link
Direct link to this question
https://www.mathworks.com/matlabcentral/answers/25381-how-to-show-partial-legend-in-figure
Commented: Julien on 8 Jun 2024 at 21:28
Accepted Answer: Dr. Seis
Open in MATLAB Online
Dear All,
I just want to show the partial part of my legend in Figure.
For example.
plot([1:10],'Color','r','DisplayName','This one');hold on;
plot([1:2:10],'Color','b','DisplayName','This two');
plot([1:3:10],'Color','k','DisplayName','This three');
Lets say I want to display only Last two or First two or first and last legend titles with proper colors.
Could anyone suggest plz.
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
Accepted Answer
Dr. Seis on 5 Jan 2012
Open in MATLAB Online
To take your first example:
h = zeros(1,3);
h(1) = plot([1:10],'Color','r','DisplayName','This one');hold on;
h(2) = plot([1:2:10],'Color','b','DisplayName','This two');
h(3) = plot([1:3:10],'Color','k','DisplayName','This three'); hold off;
legend(h(2:3)); % Only display last two legend titles
If you didn't have "DisplayName", then you would have to manually add these string to the legend:
legend(h(2:3),'This two','This three');
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
More Answers (3)
Jon on 13 Apr 2017
Edited: Jon on 13 Apr 2017
Open in MATLAB Online
Posting this here because none of the above helped me. If you don't have control of how the figure was plotted (i.e. it was buried in someone else's code), you can pull it out of the figure's children. So if you have 6 graphs and only want the legend to display a certain two, then write:
f=get(gca,'Children');
legend([f(2),f(6)],'second graph','sixth graph')
11 Comments Show 9 older commentsHide 9 older comments
Show 9 older commentsHide 9 older comments
Anand Kamlapure on 7 Sep 2017
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/25381-how-to-show-partial-legend-in-figure#comment_482652
Edited: Anand Kamlapure on 7 Sep 2017
Open in MATLAB Online
Thank you Jon, you saved me !!, Just to add, the children are pulled out in the reverse order. So you need to add following code in between, to make the legends appear in the same way as you plotted.
f=flipud(f);
%%or alternately 'f=rot90(rot90(f));'
Dave on 27 Jan 2018
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/25381-how-to-show-partial-legend-in-figure#comment_529010
Edited: Dave on 27 Jan 2018
Open in MATLAB Online
I think this is helping me, but I am unsure about the next step. Lets say I have 20 lines plotted -- 2 are red, 8 are blue, and the rest are black. When I use Jon's suggestion ["f=get(gca,'Children');"] I end up with "f" being a 20x1 Line object.
Now how do I process "f" to find the red lines? The documentation on accessing property data is either too simple or I am. My ultimate goal is to do something like this:
> red_indx = find(f.Color == 'r'); %what I want to do, does not work
> blue_indx = find(f.Color == 'b'); %what I want to do, does not work
> legend([f(red_indx(1)) f(blue_indx(1))],'Red Line','Blue Line');
so that I only have one legend entry for all the red lines and one legend entry for all the blue lines.
Can someone provide some guidance to documentation (or some example commented code) that can help me implement the generation of red_indx and blue_indx above? Nothing I have tried to get a result has worked -- I think because I really do not understand the primitive Line structure or how to work with it.
Jan on 8 Feb 2018
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/25381-how-to-show-partial-legend-in-figure#comment_533378
@Jamal Ahmad: Please use flags only to inform admins and editors about messages, which might conflict with the terms of use of this forum, e.g. by rudeness or spam. If you like a solution, vote for it.
MINATI PATRA on 27 Jul 2019
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/25381-how-to-show-partial-legend-in-figure#comment_728929
But what if I have drawn 6 curves in a fig. and want 1st, 3rd and 5th curve as dashed lines others solid lines in LEGEND with mentioning ' -- A=1' and '- A=0'.
Hunter Pruett on 25 May 2020
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/25381-how-to-show-partial-legend-in-figure#comment_863428
This is great! Thanks! :)
Sharath Yerneni on 15 Jul 2020
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/25381-how-to-show-partial-legend-in-figure#comment_937814
This is helpful? Thanks Jon
ash maison on 22 Jul 2020
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/25381-how-to-show-partial-legend-in-figure#comment_945345
Dear Jon
I just want to thank you for this very very useful comments. Unfortunately I find 99% of the comments on the MATLAB Answers completely useless at least to me.
It seems that they have forgotten that when they marketed they product to us they emphasized that unlike Python or Gnuplot, MatLab was supposed to provide user friendly interface and GUI, but non of their solutions comes with an explaination for people like me that just use this software for specific applications and have no interest in learning the fundamentals of the coding.
I don't know what is the point if I can put to figures that are pplotted seperately together and have no access to how they have been plotted but there won't be any solutions how to deal with issues like manipulating the final plot legend.
Anyway...Thanks, unlike people that actually work for MATLAB you helped alot.
Frank Selker on 24 Sep 2020
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/25381-how-to-show-partial-legend-in-figure#comment_1023112
Edited: Frank Selker on 24 Sep 2020
Agree. Matlab graphics paradigm and interface sucks.
tuncay olcer on 10 Nov 2020
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/25381-how-to-show-partial-legend-in-figure#comment_1123960
This is the perfectly working approach, Thanks Jon!
YU CHEN on 7 Jun 2021
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/25381-how-to-show-partial-legend-in-figure#comment_1569050
Perfect!Thank you very much!
Julien on 8 Jun 2024 at 21:28
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/25381-how-to-show-partial-legend-in-figure#comment_3182326
Ya da best!
Sign in to comment.
the cyclist on 5 Jan 2012
Open in MATLAB Online
Here's one way:
h1=plot([1:10],'Color','r','DisplayName','This one');hold on;
h2=plot([1:2:10],'Color','b','DisplayName','This two');
h3=plot([1:3:10],'Color','k','DisplayName','This three');
legend([h1 h3],{'Legend 1','Legend 3'})
1 Comment Show -1 older commentsHide -1 older comments
Show -1 older commentsHide -1 older comments
Sean on 5 Jan 2012
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/25381-how-to-show-partial-legend-in-figure#comment_56254
+1. This should probably be added to the FAQ.
Sign in to comment.
Patrick Kalita on 5 Jan 2012
Open in MATLAB Online
the cyclist's solution of passing plot handles to the legend command is good. But if you're feeling adventurous, you can also use the plot's Annotation property. It is described in the documentation here. For example:
h(1) = plot([1:10],'Color','r','DisplayName','This one');hold on;
h(2) = plot([1:2:10],'Color','b','DisplayName','This two');
h(3) = plot([1:3:10],'Color','k','DisplayName','This three');
set( get( get( h(2), 'Annotation'), 'LegendInformation' ), 'IconDisplayStyle', 'off' );
legend show
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
See Also
Categories
MATLABGraphicsFormatting and AnnotationLabels and AnnotationsLegend
Find more on Legend in Help Center and File Exchange
Tags
- figure
- legend
- customize legend.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office