Thread Subject: multiple legends

Subject: multiple legends

From: Daniel Walker

Date: 27 Jul, 2007 10:17:00

Message: 1 of 12

Hi all
Is there an easy way to use two different legends in the same graphic?

The following code is replacing the first legend (hfar) by the second one (hpod), despite using another handle:

hfar= legend(far,legfar, 'Location', [...]);
hpod= legend(pod,legpod, 'Location', [...]);

Thank you
Daniel

Subject: multiple legends

From: Naor Movshovitz

Date: 27 Jul, 2007 11:20:07

Message: 2 of 12

Matlab displays only one legend per axes. So your options are to manually create a text box that looks like the legend (not so easy if you're a real stricktler on appearence), or create another axes "below" and put a legend in it.

-naor

"Daniel Walker " <dwamail.nospam@gmx.ch> wrote in message <f8cgms$335$1@fred.mathworks.com>...
> Hi all
> Is there an easy way to use two different legends in the same graphic?
>
> The following code is replacing the first legend (hfar) by the second one (hpod), despite using another handle:
>
> hfar= legend(far,legfar, 'Location', [...]);
> hpod= legend(pod,legpod, 'Location', [...]);
>
> Thank you
> Daniel
>

Subject: multiple legends

From: Daniel Walker

Date: 27 Jul, 2007 14:49:02

Message: 3 of 12


Thanks...
adding additional axes works fine!
Daniel

Subject: multiple legends

From: Thomas Clerc

Date: 4 Oct, 2007 17:27:44

Message: 4 of 12


Could you please provide me a simple example where MatLab
displays a legend for the function f(x)=sin(x) and an
another legend for the two points of the function P1(pi/2;1)
and P2(pi;0).

Is it also possible to write all in the same legend box?

Subject: multiple legends

From: Adam

Date: 4 Oct, 2007 18:14:24

Message: 5 of 12

"Thomas Clerc" <thomas.clerc@unifr.ch> wrote in message
<fe37qg$716$1@fred.mathworks.com>...
>
> Could you please provide me a simple example where MatLab
> displays a legend for the function f(x)=sin(x) and an
> another legend for the two points of the function P1(pi/2;1)
> and P2(pi;0).
>
> Is it also possible to write all in the same legend box?

yes

>> x= 0:0.01:2*pi;
>> y = sin(x);
>> plot(x, y, 'k:', pi/2, 1, 'or', pi, 0, 'xg')
>> legend('sin', 'peak', 'zero')

~Adam

Subject: multiple legends

From: Thomas Clerc

Date: 5 Oct, 2007 09:49:32

Message: 6 of 12

Thank Adam, it does indeed produce a legend in one legend box.

Now, how would I do to produced to different legend boxes,
one for the function plot and one for the points.

Sorry to be so insistent.

thanj^k you inadvance

Thomas

Subject: multiple legends

From: Tony Smith

Date: 25 Oct, 2007 19:57:40

Message: 7 of 12

"Thomas Clerc" <thomas.clerc@unifr.ch> wrote in message
<fe51bc$f8p$1@fred.mathworks.com>...
> Thank Adam, it does indeed produce a legend in one
legend box.
>
> Now, how would I do to produced to different legend
boxes,
> one for the function plot and one for the points.
>
> Sorry to be so insistent.
>
> thanj^k you inadvance
>
> Thomas

To create two legends, you can create two axes and
associate each with the appropriate data. E.g.
x= 0:0.01:2*pi;
y = sin(x);
hl1 = line(x, y,'Color','k','LineStyle','--');
ax1 = gca;
set(ax1,'xlim',[0, 7],'ylim',[-1,
1],'XColor','k','YColor','k');
legend_handle1 = legend(' sin');
ax2 = axes('Position',get(ax1,'Position'),...
           'xlim',[0, 7],'ylim',[-1,1],...
           'Visible','off','Color','none');
hl2 = line(pi/2, 1,'Color','r','Marker', 'o','Parent',ax2);
hl3 = line(pi, 0,'Color','g','Marker', 'x','Parent',ax2);
legend_handle2 = legend('peak', 'zero');
set(legend_handle2, 'Color', 'none');

An issue with this is that the second legend will be place
on top of the first. You'll have to grab it with your
mouse and move it where you like.

Of course, the approach Adam suggested is much simpler and
preferred when two axes are not needed.

I came across this two-axis approach because I needed two
y-axes with multiple data records per axis. As I
understand, plotyy allows only one data record per axis.

A question I have is "Is it possible and if so, how might
I create just one box around both legends?" I could set
the show box property to off for the individual legends
and then draw one box around both by hand, but I'd like to
find an automated approach if possible.

Tony

Subject: multiple legends

From: Johan

Date: 16 Nov, 2009 12:04:02

Message: 8 of 12

"Tony Smith" <tony.smith@gdc4s.com> wrote in message <ffqsfk$lk6$1@fred.mathworks.com>...

> To create two legends, you can create two axes and
> associate each with the appropriate data. E.g.
Thanks, this code works nicely =)
 
> An issue with this is that the second legend will be place
> on top of the first. You'll have to grab it with your
> mouse and move it where you like.

This is easly solved by setting the Location property, e.g.
legend_handle2 = legend('peak', 'zero','Location','SouthEast');

//Johan

Subject: multiple legends

From: sarrah

Date: 14 Jan, 2010 10:28:03

Message: 9 of 12

"Tony Smith" <tony.smith@gdc4s.com> wrote in message <ffqsfk$lk6$1@fred.mathworks.com>...
> "Thomas Clerc" <thomas.clerc@unifr.ch> wrote in message
> <fe51bc$f8p$1@fred.mathworks.com>...
> > Thank Adam, it does indeed produce a legend in one
> legend box.
> >
> > Now, how would I do to produced to different legend
> boxes,
> > one for the function plot and one for the points.
> >
> > Sorry to be so insistent.
> >
> > thanj^k you inadvance
> >
> > Thomas
>
> To create two legends, you can create two axes and
> associate each with the appropriate data. E.g.
> x= 0:0.01:2*pi;
> y = sin(x);
> hl1 = line(x, y,'Color','k','LineStyle','--');
> ax1 = gca;
> set(ax1,'xlim',[0, 7],'ylim',[-1,
> 1],'XColor','k','YColor','k');
> legend_handle1 = legend(' sin');
> ax2 = axes('Position',get(ax1,'Position'),...
> 'xlim',[0, 7],'ylim',[-1,1],...
> 'Visible','off','Color','none');
> hl2 = line(pi/2, 1,'Color','r','Marker', 'o','Parent',ax2);
> hl3 = line(pi, 0,'Color','g','Marker', 'x','Parent',ax2);
> legend_handle2 = legend('peak', 'zero');
> set(legend_handle2, 'Color', 'none');
>
> An issue with this is that the second legend will be place
> on top of the first. You'll have to grab it with your
> mouse and move it where you like.
>
> Of course, the approach Adam suggested is much simpler and
> preferred when two axes are not needed.
>
> I came across this two-axis approach because I needed two
> y-axes with multiple data records per axis. As I
> understand, plotyy allows only one data record per axis.
>
> A question I have is "Is it possible and if so, how might
> I create just one box around both legends?" I could set
> the show box property to off for the individual legends
> and then draw one box around both by hand, but I'd like to
> find an automated approach if possible.
>
> Tony
-----
Hi Tony,
Do you know how to separate this long legend into two boxes of legend?
i.e: legend('a=1','b=2','c=3','d=7', 'e=8','f=9','g=10','h=77');

If I created another legend, it will overwrite the existing legend. From the previous discussion, you add the position. My plot is on log and there are a lots of data than I mentioned as above and I don't know how to set the position of the axes.

I want to split the legend (a to d) in one box and (e-h) into another box. How can I program it?

Others who know the answer, please kindly contribute your opinion. Thanks in advance.
-Sarrah

Subject: multiple legends

From: Ken

Date: 23 Mar, 2010 12:07:22

Message: 10 of 12

Hi Sarah,

try this:

x1 = [0:.1:40];
y1 = 4.*cos(x1)./(x1+2);
x2 = [1:.2:20];
y2 = x2.^2./x2.^3;

hl1 = line(x1,y1,'Color','r');
ax1 = gca;
set(ax1,'XColor','r','YColor','r')

ax2 = axes('Position',get(ax1,'Position'),...
           'XAxisLocation','top',...
           'YAxisLocation','right',...
           'Color','none',...
           'XColor','k','YColor','k');
       
hl2 = line(x2,y2,'Color','k','Parent',ax2);

it works.

-ken

"sarrah " <gto_girlz83@yahoo.com> wrote in message <himrjj$qvn$1@fred.mathworks.com>...
> "Tony Smith" <tony.smith@gdc4s.com> wrote in message <ffqsfk$lk6$1@fred.mathworks.com>...
> > "Thomas Clerc" <thomas.clerc@unifr.ch> wrote in message
> > <fe51bc$f8p$1@fred.mathworks.com>...
> > > Thank Adam, it does indeed produce a legend in one
> > legend box.
> > >
> > > Now, how would I do to produced to different legend
> > boxes,
> > > one for the function plot and one for the points.
> > >
> > > Sorry to be so insistent.
> > >
> > > thanj^k you inadvance
> > >
> > > Thomas
> >
> > To create two legends, you can create two axes and
> > associate each with the appropriate data. E.g.
> > x= 0:0.01:2*pi;
> > y = sin(x);
> > hl1 = line(x, y,'Color','k','LineStyle','--');
> > ax1 = gca;
> > set(ax1,'xlim',[0, 7],'ylim',[-1,
> > 1],'XColor','k','YColor','k');
> > legend_handle1 = legend(' sin');
> > ax2 = axes('Position',get(ax1,'Position'),...
> > 'xlim',[0, 7],'ylim',[-1,1],...
> > 'Visible','off','Color','none');
> > hl2 = line(pi/2, 1,'Color','r','Marker', 'o','Parent',ax2);
> > hl3 = line(pi, 0,'Color','g','Marker', 'x','Parent',ax2);
> > legend_handle2 = legend('peak', 'zero');
> > set(legend_handle2, 'Color', 'none');
> >
> > An issue with this is that the second legend will be place
> > on top of the first. You'll have to grab it with your
> > mouse and move it where you like.
> >
> > Of course, the approach Adam suggested is much simpler and
> > preferred when two axes are not needed.
> >
> > I came across this two-axis approach because I needed two
> > y-axes with multiple data records per axis. As I
> > understand, plotyy allows only one data record per axis.
> >
> > A question I have is "Is it possible and if so, how might
> > I create just one box around both legends?" I could set
> > the show box property to off for the individual legends
> > and then draw one box around both by hand, but I'd like to
> > find an automated approach if possible.
> >
> > Tony
> -----
> Hi Tony,
> Do you know how to separate this long legend into two boxes of legend?
> i.e: legend('a=1','b=2','c=3','d=7', 'e=8','f=9','g=10','h=77');
>
> If I created another legend, it will overwrite the existing legend. From the previous discussion, you add the position. My plot is on log and there are a lots of data than I mentioned as above and I don't know how to set the position of the axes.
>
> I want to split the legend (a to d) in one box and (e-h) into another box. How can I program it?
>
> Others who know the answer, please kindly contribute your opinion. Thanks in advance.
> -Sarrah

Subject: multiple legends

From: John

Date: 12 Jul, 2010 18:32:05

Message: 11 of 12

"Naor Movshovitz" <lazy_n@yahoo.com> wrote in message <f8ckd7$q5b$1@fred.mathworks.com>...
> Matlab displays only one legend per axes. So your options are to manually create a text box that looks like the legend (not so easy if you're a real stricktler on appearence), or create another axes "below" and put a legend in it.
>
> -naor
>
> "Daniel Walker " <dwamail.nospam@gmx.ch> wrote in message <f8cgms$335$1@fred.mathworks.com>...
> > Hi all
> > Is there an easy way to use two different legends in the same graphic?
> >
> > The following code is replacing the first legend (hfar) by the second one (hpod), despite using another handle:
> >
> > hfar= legend(far,legfar, 'Location', [...]);
> > hpod= legend(pod,legpod, 'Location', [...]);
> >
> > Thank you
> > Daniel
> >
>

What exactly do you mean by "below". And how do you add an axes?

Subject: multiple legends

From: Raj Sodhi

Date: 10 Mar, 2011 01:00:05

Message: 12 of 12

Hi Ken,

Nice example.
You forgot to include the 'legend' statements.

Yours,

Raj

=========
figure(1) ; clf ;

x1 = [0:.1:40];
y1 = 4.*cos(x1)./(x1+2);
x2 = [1:.2:20];
y2 = x2.^2./x2.^3;

hl1 = line(x1,y1,'Color','r');
ax1 = gca;
set(ax1,'XColor','r','YColor','r')
legend('legend1') ;

ax2 = axes('Position',get(ax1,'Position'),...
           'XAxisLocation','top',...
           'YAxisLocation','right',...
           'Color','none',...
           'XColor','k','YColor','k');
       
hl2 = line(x2,y2,'Color','k','Parent',ax2);
legend('legend2','location','SE') ;
============


"Ken " <kk61@cornell.edu> wrote in message <hoaatq$dnb$1@fred.mathworks.com>...
> Hi Sarah,
>
> try this:
>
> x1 = [0:.1:40];
> y1 = 4.*cos(x1)./(x1+2);
> x2 = [1:.2:20];
> y2 = x2.^2./x2.^3;
>
> hl1 = line(x1,y1,'Color','r');
> ax1 = gca;
> set(ax1,'XColor','r','YColor','r')
>
> ax2 = axes('Position',get(ax1,'Position'),...
> 'XAxisLocation','top',...
> 'YAxisLocation','right',...
> 'Color','none',...
> 'XColor','k','YColor','k');
>
> hl2 = line(x2,y2,'Color','k','Parent',ax2);
>
> it works.
>
> -ken
>

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
one box Tony Smith 25 Oct, 2007 16:00:27
multiple legends Tony Smith 25 Oct, 2007 16:00:26
multiple legend... Thomas Clerc 5 Oct, 2007 05:50:04
multiple legend Thomas Clerc 4 Oct, 2007 13:30:25
handle Daniel Walker 27 Jul, 2007 06:20:05
legend Daniel Walker 27 Jul, 2007 06:20:05
image Daniel Walker 27 Jul, 2007 06:20:05
plot Daniel Walker 27 Jul, 2007 06:20:05
rssFeed for this Thread

Contact us at files@mathworks.com