 # legend and twiny

Hi,

In the code below legend(label) for ax2 is shown as ax2.legend() is called after ax1.legend().

Both plots do show up as expected.

What should I do to get the legends for both plots.

Thank you

Raju

···

import matplotlib.pyplot as plt

import numpy as py

x=np.arange(0.0,np.e,0.02)

y1=np.exp(-x)

y2=np.exp(x)

fig = plt.figure()

ax2=ax1.twinx()

ax1.plot(x,y1,label=“First”)

ax2.plot(x,y2,label=“Second”)

ax1.legend()

ax2.legend()

plt.show()

Hemantharaju Subbanna
408-857-9414(cell)

Raju, I think ax1.legend() is appearing under ax2.legend(). The problem is that the legend() function works on a single axes object, but we technically have two axes objects.

I wonder if there needs to be some additional thought put into twinx and twiny and how it works with other aspects of matplotlib?

Ben Root

···

On Fri, Sep 24, 2010 at 4:08 PM, Raju Subban <rajusubbanna@…120…287…> wrote:

Hi,

In the code below legend(label) for ax2 is shown as ax2.legend() is called after ax1.legend().

Both plots do show up as expected.

What should I do to get the legends for both plots.

Thank you

Raju

Try something like

handles1, labels1 = ax1.get_legend_handles_labels()
handles2, labels2 = ax2.get_legend_handles_labels()
ax2.legend(handles1+handles2, labels1+labels2)

Also, see

http://matplotlib.sourceforge.net/users/legend_guide.html

Regards,

-JJ

···

On Sat, Sep 25, 2010 at 6:08 AM, Raju Subban <rajusubbanna@...287...> wrote:

Hi,
In the code below legend(label) for ax2 is shown as ax2.legend() is called
after ax1.legend().
Both plots do show up as expected.

What should I do to get the legends for both plots.

Thank you
Raju
--------------------------------------
import matplotlib.pyplot as plt
import numpy as py
x=np.arange(0.0,np.e,0.02)
y1=np.exp(-x)
y2=np.exp(x)
fig = plt.figure()
ax2=ax1.twinx()
ax1.plot(x,y1,label="First")
ax2.plot(x,y2,label="Second")
ax1.legend()
ax2.legend()
plt.show()

--
Hemantharaju Subbanna
408-857-9414(cell)

------------------------------------------------------------------------------
Start uncovering the many advantages of virtual appliances
and start using them to simplify application deployment and
accelerate your shift to cloud computing.
http://p.sf.net/sfu/novell-sfdev2dev
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users