move legend to the foreground: how to use set_zorder() correctly?

Hi All,

I am plotting on two different y-axes: one on the left (ax1) and one on the right (ax2). Both share the same x-axes. The problem I am facing relates back to the zorder of the legend (at least, that is what I think): I want it to be on the foreground at all times. In order to do so, I change the zorder of the ax1.legend (left y axes) such that the ax2.plots (right y-axes) are under ax1.legend. However, that doesn't work: all the plots on the right axes (so using ax2) end up above the legend 1 on the left, despite having a lower zorder.

Note that I am also giving the grids on both ax1 and ax2 a lower zorder value compared to the legends, but the grid still ends up on top of legend 1 on the left.

# version info:

# Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41) [GCC 4.4.3] on linux2

# NumPy 1.6.1, Matplotlib 1.1.0

import pylab as plt

import numpy as np

# plotting on the left y-axes,

ax1 = plt.axes(zorder=10)

ax1.plot(range(0,5,1), 'r', label='ax1 ax1 ax1 ax1', zorder=11)

ax1.plot(np.arange(3,4.1,1), 'r--', label='ax1 ax1 ax1 ax1', zorder=12)

gr1 = ax1.grid(zorder=13)

# legend of the left y-axes, force high zorder

leg1 = ax1.legend(loc='upper left')

leg1.set_zorder(30)

# plotting on the right y-axes,

ax2 = plt.twinx()

ax2.set_zorder(20)

ax2.plot(range(4,-1,-1), 'b', label='ax2 ax2 ax2 ax2', zorder=21)

ax2.plot(np.arange(4,2.9,-1), np.arange(3,4.1,1), 'b--',

label='ax2 ax2 ax2 ax2', zorder=22)

gr2 = ax2.grid(zorder=23)

# legend of the right y-axes, force high zorder

leg2 = ax2.legend(loc='upper right')

leg2.set_zorder(40)

print '======= zorder:'

print ' ax1: %i' % ax1.get_zorder()

print ' ax2: %i' % ax2.get_zorder()

print 'leg1: %i' % leg1.get_zorder()

print 'leg2: %i' % leg2.get_zorder()

What am I missing here?
Thanks,
David

zorders are only meaningful among objects in a same axes.

An easy workaround is to move the legend in ax1 to ax2.

   ax2.add_artist(leg1)
   ax1.legend = None

Regards,

-JJ

ยทยทยท

On Thu, Mar 22, 2012 at 10:13 PM, David Verelst <david.verelst@...287...> wrote:

Hi All,

I am plotting on two different y-axes: one on the left (ax1) and one on
the right (ax2). Both share the same x-axes. The problem I am facing
relates back to the zorder of the legend (at least, that is what I
think): I want it to be on the foreground at all times. In order to do
so, I change the zorder of the ax1.legend (left y axes) such that the
ax2.plots (right y-axes) are under ax1.legend. However, that doesn't
work: all the plots on the right axes (so using ax2) end up above the
legend 1 on the left, despite having a lower zorder.

Note that I am also giving the grids on both ax1 and ax2 a lower zorder
value compared to the legends, but the grid still ends up on top of
legend 1 on the left.

# version info:

# Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41) [GCC 4.4.3] on linux2

# NumPy 1.6.1, Matplotlib 1.1.0

import pylab as plt

import numpy as np

# plotting on the left y-axes,

ax1 = plt.axes(zorder=10)

ax1.plot(range(0,5,1), 'r', label='ax1 ax1 ax1 ax1', zorder=11)

ax1.plot(np.arange(3,4.1,1), 'r--', label='ax1 ax1 ax1 ax1', zorder=12)

gr1 = ax1.grid(zorder=13)

# legend of the left y-axes, force high zorder

leg1 = ax1.legend(loc='upper left')

leg1.set_zorder(30)

# plotting on the right y-axes,

ax2 = plt.twinx()

ax2.set_zorder(20)

ax2.plot(range(4,-1,-1), 'b', label='ax2 ax2 ax2 ax2', zorder=21)

ax2.plot(np.arange(4,2.9,-1), np.arange(3,4.1,1), 'b--',

label='ax2 ax2 ax2 ax2', zorder=22)

gr2 = ax2.grid(zorder=23)

# legend of the right y-axes, force high zorder

leg2 = ax2.legend(loc='upper right')

leg2.set_zorder(40)

print '======= zorder:'

print ' ax1: %i' % ax1.get_zorder()

print ' ax2: %i' % ax2.get_zorder()

print 'leg1: %i' % leg1.get_zorder()

print 'leg2: %i' % leg2.get_zorder()

What am I missing here?
Thanks,
David

------------------------------------------------------------------------------
This SF email is sponsosred by:
Try Windows Azure free for 90 days Click Here
http://p.sf.net/sfu/sfd2d-msazure
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options