Thread Subject: multiple legends

Subject: multiple legends

From: Daniel Walker

Date: 27 Jul, 2007 10:17:00

Message: 1 of 7

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 7

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 7


Thanks...
adding additional axes works fine!
Daniel

Subject: multiple legends

From: Thomas Clerc

Date: 4 Oct, 2007 17:27:44

Message: 4 of 7


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 7

"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 7

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 7

"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

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 boxes 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

Public Submission Policy

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.

Contact us at files@mathworks.com