How to show partial legend in figure
Show older comments
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
More Answers (3)
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
Anand Kamlapure
on 7 Sep 2017
Edited: Anand Kamlapure
on 7 Sep 2017
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));'
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
@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
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
This is great! Thanks! :)
Sharath Yerneni
on 15 Jul 2020
This is helpful? Thanks Jon
ash maison
on 22 Jul 2020
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
Edited: Frank Selker
on 24 Sep 2020
Agree. Matlab graphics paradigm and interface sucks.
tuncay olcer
on 10 Nov 2020
This is the perfectly working approach, Thanks Jon!
YU CHEN
on 7 Jun 2021
Perfect!Thank you very much!
Julien
on 8 Jun 2024
Ya da best!
the cyclist
on 5 Jan 2012
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
Sean de Wolski
on 5 Jan 2012
+1. This should probably be added to the FAQ.
Patrick Kalita
on 5 Jan 2012
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
Categories
Find more on Legend in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!