图例中的误差线。

ely*_*ely 1 plot matplotlib

我正在绘制几个带有 y 错误的数据点,并且我不希望图例中包含错误栏。

 p1=ax.errorbar(x,y, yerr=[ydown,yup], color='blue', fmt='s', ecolor='blue')
 p2=ax.errorbar(x1,y1, yerr=[y1down,y1up], color='black', fmt='.', ecolor='black')
 ax.legend([p1,p2],['data1','data2'], loc='upper left', numpoints=1)
Run Code Online (Sandbox Code Playgroud)

我的问题与上一个问题类似: Matplotlib: Don't show errorbars in legend 但我还没有找到解决方案。谢谢。

Dav*_*ker 5

您可以修改图例处理程序。请参阅matplotlib 的图例指南。调整您的示例,这可以是:

# get handles
handles, labels = ax.get_legend_handles_labels()
# remove the errorbars
handles = [h[0] for h in handles]
# use them in the legend
ax.legend(handles, labels, loc='upper left', numpoints=1)
Run Code Online (Sandbox Code Playgroud)