How to show separate legend for each subplot

Hi,

I am wondering is there a way one could show a separate legend for each subplot in a figure? I would really really appreciate any ideas on this.

Thanks,

Omer

Hi,

I am wondering is there a way one could show a separate legend for each subplot in a figure? I would really really appreciate any ideas on this.
`~~~~~~~~~~~~~~~~~~~~~~~~~'

Omer,

Give this a shot:
# code --------->
import matplotlib.pyplot as pl
fig = pl.figure()
ax1 = fig.add_subplot(1,2,1)
ax2 = fig.add_subplot(1,2,2)

ax1.plot([1,2], [3,4], 'ko', label='First Axes')
ax2.plot([2,3], [4,5[, 'bs', label='Second Axes')

ax1.legend()
ax2.legend()
# <------------- code

There's also an example in the Examples section of the MPL website:
http://matplotlib.sourceforge.net/examples/pylab_examples/legend_demo3.html

Hope that helps,
-paul

···

From: Omer Khalid [mailto:Omer.Khalid@…1758…]
Sent: Wednesday, March 31, 2010 6:20 AM
To: Matplotlib Users
Subject: [Matplotlib-users] How to show separate legend for each subplot

Hi Paul,

Thanks for your prompt response. It did help :slight_smile:

But it seems that axis don’t seems to accept title, xlable and ylable values any more. Any ideas on that?

Cheers

Omer

···

On Wed, Mar 31, 2010 at 17:43, <PHobson@…2850…> wrote:

From: Omer Khalid [mailto:Omer.Khalid@…1758…]

Sent: Wednesday, March 31, 2010 6:20 AM

To: Matplotlib Users

Subject: [Matplotlib-users] How to show separate legend for each subplot

Hi,

I am wondering is there a way one could show a separate legend for each subplot in a figure? I would really really appreciate any ideas on this.

`~~~~~~~~~~~~~~~~~~~~~~~~~’

Omer,

Give this a shot:

code --------->

import matplotlib.pyplot as pl

fig = pl.figure()

ax1 = fig.add_subplot(1,2,1)

ax2 = fig.add_subplot(1,2,2)

ax1.plot([1,2], [3,4], ‘ko’, label=‘First Axes’)

ax2.plot([2,3], [4,5[, ‘bs’, label=‘Second Axes’)

ax1.legend()

ax2.legend()

<------------- code

There’s also an example in the Examples section of the MPL website:

http://matplotlib.sourceforge.net/examples/pylab_examples/legend_demo3.html

Hope that helps,

-paul

Hi Paul,
Thanks for your prompt response. It did help :slight_smile:
But it seems that axis don't seems to accept title, xlable and ylable values
any more. Any ideas on that?

If you're trying to use the functions plt.title() and plt.xlabel(),
they only work on the current axes object (basically, the most
recently created one). You'll want to try to use methods on the axes
themselves:

ax1.set_title('ax1 title')
ax1.set_xlabel('xlabel')

ax2.set_title('ax2 title')
ax2.set_xlabel('xlabel')

Ryan

···

On Wed, Mar 31, 2010 at 10:21 AM, Omer Khalid <Omer.Khalid@...1758...> wrote:

On Wed, Mar 31, 2010 at 17:43, <PHobson@...2850...> wrote:

From: Omer Khalid [mailto:Omer.Khalid@…1758…]
Sent: Wednesday, March 31, 2010 6:20 AM
To: Matplotlib Users
Subject: [Matplotlib-users] How to show separate legend for each subplot

Hi,

I am wondering is there a way one could show a separate legend for each
subplot in a figure? I would really really appreciate any ideas on this.
`~~~~~~~~~~~~~~~~~~~~~~~~~'

Omer,

Give this a shot:
# code --------->
import matplotlib.pyplot as pl
fig = pl.figure()
ax1 = fig.add_subplot(1,2,1)
ax2 = fig.add_subplot(1,2,2)

ax1.plot([1,2], [3,4], 'ko', label='First Axes')
ax2.plot([2,3], [4,5[, 'bs', label='Second Axes')

ax1.legend()
ax2.legend()
# <------------- code

There's also an example in the Examples section of the MPL website:

http://matplotlib.sourceforge.net/examples/pylab_examples/legend_demo3.html

Hope that helps,
-paul

------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options

--
Ryan May
Graduate Research Assistant
School of Meteorology
University of Oklahoma