Matplotlib:不要在图例中显示错误栏

bio*_*ime 16 matplotlib

我正在使用x和y错误绘制一系列数据点,但不希望错误栏包含在图例中(仅限标记).有办法吗?

如何避免图例中的错误栏?

例:

import matplotlib.pyplot as plt
import numpy as np
subs=['one','two','three']
x=[1,2,3]
y=[1,2,3]
yerr=[2,3,1]
xerr=[0.5,1,1]
fig,(ax1)=plt.subplots(1,1)
for i in np.arange(len(x)):
    ax1.errorbar(x[i],y[i],yerr=yerr[i],xerr=xerr[i],label=subs[i],ecolor='black',marker='o',ls='')
ax1.legend(loc='upper left', numpoints=1)
fig.savefig('test.pdf', bbox_inches=0)
Run Code Online (Sandbox Code Playgroud)

Dav*_*ker 19

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

import matplotlib.pyplot as plt
import numpy as np

subs=['one','two','three']
x=[1,2,3]
y=[1,2,3]
yerr=[2,3,1]
xerr=[0.5,1,1]
fig,(ax1)=plt.subplots(1,1)

for i in np.arange(len(x)):
    ax1.errorbar(x[i],y[i],yerr=yerr[i],xerr=xerr[i],label=subs[i],ecolor='black',marker='o',ls='')

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


plt.show()
Run Code Online (Sandbox Code Playgroud)

这产生了

输出图像