Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib.legend.set_zorder() doesn't work in ipython notebook

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.sstatic.net/MOekP.png

I'm using matplotlib 1.3.1 on ipython 3.1.0

like image 453
andrea Avatar asked Apr 28 '26 22:04

andrea


1 Answers

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)
like image 175
tmdavison Avatar answered Apr 30 '26 11:04

tmdavison



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!