2015-11-11 37 views
2

這是一個很快的事情,這讓我非常煩惱。我遵循strange matplotlib zorder behavior with legend and errorbar中的指示,但它不起作用。這裏是一個非常簡單的工作示例matplotlib.legend.set_zorder()在ipython筆記本中不起作用

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) 

如果你試試這個,你會看到那個傳說依然是曲線的下方,儘管zorder ING。有誰知道爲什麼?

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

我使用matplotlib 1.3.1 IPython中3.1.0

+1

對我來說,傳說和曲線似乎是正確的順序,但圖例矩形沒有不透明的背景。我用'matplotlib 1.4.3'試過這個圖例,圖例是不透明的。 – cel

+0

也嘗試過'matplotlib 1.3.1',這對我也很有效。也許後端問題? – cel

回答

1

從@ CEL的評論繼,我同意你需要一個不透明的背景添加到傳奇。嘗試添加:

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

它也可以設置legend.facecolormatplotlibrc或通過plt.rcParams


或者確保圖例框的fill設置爲True

print leg.get_frame().get_fill() 

如果打印False,請嘗試

leg.get_frame().set_fill(True) 
+0

感謝您的快速回答。我得到'AttributeError:'NoneType'對象沒有屬性'get_frame'' – andrea

+0

啊好的。我想你需要在設置'leg = plt.legend()'後設置'zorder'。看到我的編輯 – tom

+0

是的,那是神奇的。你的功夫強大的湯姆:)非常感謝你 – andrea