3

here's a quick thing which is quite annoying me. I've followed the indications found in strange matplotlib zorder behavior with legend and errorbar but it doesn't work. Here's a very brief working example

x = np.linspace(0,10) 
y = (x-5)**2 
plt.plot(x,y,label='test curve',zorder=100)
plt.legend(loc='center right').set_zorder(102)

If you try this you will see that the legend is still underneath the curve, despite the zordering. Does anyone know why?

https://i.stack.imgur.com/MOekP.png

I'm using matplotlib 1.3.1 on ipython 3.1.0

Community
  • 1
  • 1
andrea
  • 525
  • 1
  • 5
  • 21
  • 1
    To me it seems that legend and curve are in the right order, but the legend rectangle does not have an opaque background. I tried this with `matplotlib 1.4.3` and the legend is opaque. – cel Nov 11 '15 at 11:26
  • Also tried `matplotlib 1.3.1` and this works as well for me. Maybe a backend issue? – cel Nov 11 '15 at 11:41

1 Answers1

4

Following on from @cel's comment, I agree you need to add an opaque background to the legend. Try adding:

leg=plt.legend(loc='center right')
leg.set_zorder(102)
leg.get_frame().set_facecolor('w')

Its also possible to set legend.facecolor in your matplotlibrc or via plt.rcParams


Alternatively make sure the fill of the legend frame is set to True:

print leg.get_frame().get_fill()

If that prints False, try

leg.get_frame().set_fill(True)
tmdavison
  • 64,360
  • 12
  • 187
  • 165