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

标签 matplotlib

我正在绘制一系列带有 x 和 y 误差的数据点,但不希望将误差条包含在图例中(仅标记)。有没有办法这样做?

How to avoid errorbars in legend?

例子:

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)

最佳答案

您可以修改图例处理程序。见 legend guide of 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()

这产生

output image

关于Matplotlib:不要在图例中显示错误栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14297714/

相关文章:

python - 决策树分类器sklearn中节点的不同颜色表示什么?

python - 在 matplotlib 中放大插图而不重新绘制数据

python - 在Python中绘制椭圆轨道(使用numpy、matplotlib)

python - 仅在 matplotlib 中的大陆上绘制

Python、QT 和 matplotlib 散点图与 blitting

python - 使用 python matplotlib 用线绘制非连续数据

Python for 循环在 3D 时总是绘制同一条线(使用 matplotlib)

python - 更改直方图数据的 bin 大小

python - 在 matplotlib 中显示一条线,以便我的结果与我的教科书相匹配

python - 为什么保存按钮在 matplotlib 图上不起作用?