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 zorder
ing. Does anyone know why?
I'm using matplotlib 1.3.1 on ipython 3.1.0
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.