簡體   English   中英

matplotlib-具有錯誤軸對象的多軸圖例

[英]matplotlib - Legend with multiple axes with errorbar object

我很難在具有兩個軸集和一個錯誤欄的繪圖上獲取圖例。 代碼如下:

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不是錯誤欄圖,則此方法有效。 有什么建議可讓您將所有地塊都放在一個盒子中的圖例?

非常感謝。

使用此問題中的代碼應該可以:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM