How to show partial legend in figure

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.

 Accepted Answer

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

More Answers (3)

Jon
Jon on 13 Apr 2017
Edited: Jon on 13 Apr 2017
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

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
Dave on 27 Jan 2018
Edited: Dave on 27 Jan 2018
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.
@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.
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'.
This is great! Thanks! :)
This is helpful? Thanks Jon
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
Frank Selker on 24 Sep 2020
Edited: Frank Selker on 24 Sep 2020
Agree. Matlab graphics paradigm and interface sucks.
This is the perfectly working approach, Thanks Jon!
Perfect!Thank you very much!
Ya da best!

Sign in to comment.

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'})
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

Asked:

on 5 Jan 2012

Commented:

on 8 Jun 2024

Community Treasure Hunt

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

Start Hunting!