Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

multiple legends

641 views
Skip to first unread message

Daniel Walker

unread,
Jul 27, 2007, 6:17:00 AM7/27/07
to
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

Naor Movshovitz

unread,
Jul 27, 2007, 7:20:07 AM7/27/07
to
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...@gmx.ch> wrote in message <f8cgms$335$1...@fred.mathworks.com>...

Daniel Walker

unread,
Jul 27, 2007, 10:49:02 AM7/27/07
to

Thanks...
adding additional axes works fine!
Daniel

Thomas Clerc

unread,
Oct 4, 2007, 1:27:44 PM10/4/07
to

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?


Adam

unread,
Oct 4, 2007, 2:14:24 PM10/4/07
to
"Thomas Clerc" <thomas...@unifr.ch> wrote in message
<fe37qg$716$1...@fred.mathworks.com>...

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

Thomas Clerc

unread,
Oct 5, 2007, 5:49:32 AM10/5/07
to
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

Tony Smith

unread,
Oct 25, 2007, 3:57:40 PM10/25/07
to
"Thomas Clerc" <thomas...@unifr.ch> wrote in message
<fe51bc$f8p$1...@fred.mathworks.com>...

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

Johan

unread,
Nov 16, 2009, 7:04:02 AM11/16/09
to
"Tony Smith" <tony....@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

sarrah

unread,
Jan 14, 2010, 5:28:03 AM1/14/10
to
"Tony Smith" <tony....@gdc4s.com> wrote in message <ffqsfk$lk6$1...@fred.mathworks.com>...
-----
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

Ken

unread,
Mar 23, 2010, 8:07:22 AM3/23/10
to
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_g...@yahoo.com> wrote in message <himrjj$qvn$1...@fred.mathworks.com>...

John

unread,
Jul 12, 2010, 2:32:05 PM7/12/10
to
"Naor Movshovitz" <laz...@yahoo.com> wrote in message <f8ckd7$q5b$1...@fred.mathworks.com>...

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

Raj Sodhi

unread,
Mar 9, 2011, 8:00:05 PM3/9/11
to
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 " <kk...@cornell.edu> wrote in message <hoaatq$dnb$1...@fred.mathworks.com>...

Henri

unread,
May 1, 2012, 2:33:07 PM5/1/12
to
"Daniel Walker" wrote in message <f8cgms$335$1...@fred.mathworks.com>...
Hi,

You can manage multiple legends by using handles for plots and brackets in the legend function. See example below:
figure(1), hold on
plot(x1,y1,propertyname,propertyvalue)
h2=plot(x2,y2,propertyname,propertyvalue)
plot(x3,y3,propertyname,propertyvalue)
h4=plot(x4,y4,propertyname,propertyvalue)
plot(x5,y5,propertyname,propertyvalue)
plot(x6,y6,propertyname,propertyvalue)
h7=plot(x7,y7,propertyname,propertyvalue)
hold off
legend([h2 h4],'string2','string4'),
or legend([h2 h4 h7],'string2','string4','string7') if you need a legend for h7 too

You can create other figures and then come back to figure(1) to add some plots and the legend you need without loosing the previous ones by repeating them in the legend function.

figure(1), hold on
plot(x8,y8,propertyname,propertyvalue)
h9=plot(x9,y9,propertyname,propertyvalue)
plot(x10,y10,propertyname,propertyvalue)
hold off
legend([h2 h4 h7 h9],'string2','string4','string7','string9')

ahmed

unread,
Jan 5, 2015, 9:26:17 AM1/5/15
to
"Daniel Walker" wrote in message <f8cgms$335$1...@fred.mathworks.com>...
0 new messages