[matplotib-users] missing legend-entry using twinx()

Hi,

I'm plotting two y-axis with the twinx() command. But when I'm
creating my legend there is only one entry in the legend-box instead
of two.
Here is my code:

grafik1 = plt.figure()

ax1 = grafik1.add_subplot(111)
ax1.plot(TIME, BTM, color='red', label='Biomass')
ax1.set_ylabel('Biomass')

ax2 = ax1.twinx()
ax2.plot(TIME, FLUOR, label='Productivity')
ax2.set_ylabel('Produktivity')
ax2.set_xlabel('Time')

grafik1_legend = legend(loc=10)

This is because "legend" creates a legend only in the current axes.
Note that "twinx" creates a separate axes.

You need to manually specify which plot items to show in the legend.

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

Or, you may use axes_grid.parasite_axes which does this job for you
(but do not use this toolkit blindly but read the documentation)

http://matplotlib.sourceforge.net/examples/axes_grid/demo_parasite_axes2.html

-JJ

ยทยทยท

On Thu, Jan 7, 2010 at 9:19 AM, Manuel Wittchen <manuel.wittchen@...287...> wrote:

Hi,

I'm plotting two y-axis with the twinx() command. But when I'm
creating my legend there is only one entry in the legend-box instead
of two.
Here is my code:

grafik1 = plt.figure()

ax1 = grafik1.add_subplot(111)
ax1.plot(TIME, BTM, color='red', label='Biomass')
ax1.set_ylabel('Biomass')

ax2 = ax1.twinx()
ax2.plot(TIME, FLUOR, label='Productivity')
ax2.set_ylabel('Produktivity')
ax2.set_xlabel('Time')

grafik1_legend = legend(loc=10)

------------------------------------------------------------------------------
This SF.Net email is sponsored by the Verizon Developer Community
Take advantage of Verizon's best-in-class app development support
A streamlined, 14 day to market process makes app distribution fast and easy
Join now and get one step closer to millions of Verizon customers
http://p.sf.net/sfu/verizon-dev2dev
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users