2012-05-11 88 views
1

我無法在帶有兩個座標軸集和一個錯誤欄的圖上獲得圖例。代碼如下:matplotlib - 帶有多個軸的圖例,帶有錯誤欄對象

rect = 0.1, 0.1, 0.8, 0.8 
fig = p.figure() 

ax1 = fig.add_axes(rect) 
errorplot = ax1.errorbar(x, y, yerr = yerr, fmt = "o", label = r"errorbar") 
plot1 = ax1.plot(x1, y1, "^", color = "#00FF7F", label = r"plot") 
ax1.yaxis.tick_left() 
ax1.xaxis.tick_bottom() 
ax2 = fig.add_axes(rect, frameon=False) 
ax2.yaxis.tick_right() 
ax2.yaxis.set_label_position('right') 
ax2.xaxis.tick_top() 
ax2.xaxis.set_label_position('top') 
plot2 = ax2.plot(z, al, color = "#CD8500", linewidth =2, label= r"plot2") 
k = list(errorplot[:1]) 
plots = k + plot1 + plot2 
labels = [l.get_label() for l in k] 
ax1.legend(plots, labels, loc = 2) 
ax1.legend(loc = 2) 
p.savefig("test.pdf") 

如果plot1不是誤差線圖,則此方法有效。任何建議,以獲得一個盒子裏的所有情節傳奇?

非常感謝。

回答

1

使用代碼從this question應該工作:

h1, l1 = ax1.get_legend_handles_labels() 
h2, l2 = ax2.get_legend_handles_labels() 
ax1.legend(h1+h2, l1+l2, loc=2)