简体   繁体   English

matplotlib-具有错误轴对象的多轴图例

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

I am having trouble to get legends on a plot with two axes sets and an errorbar. 我很难在具有两个轴集和一个错误栏的绘图上获取图例。 The code goes as follows: 代码如下:

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")

This works if plot1 is not an errorbar plot. 如果plot1不是错误栏图,则此方法有效。 Any recommendations to get a legend with all plots in one box? 有什么建议可让您将所有地块都放在一个盒子中的图例?

Thanks a lot in advance. 非常感谢。

Using the code from this question should work: 使用此问题中的代码应该可以:

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