Python Barh Errorbar 在间隔开始时绘制

问题描述 投票:0回答:1

我有一个关于 pyplot.barh 错误栏的问题。

我想在间隔的左侧包含错误栏,而不仅仅是在右侧(在图片中,右侧有错误栏,我希望左侧有相同的错误栏(相同的xerr)...

我使用的代码如下

plt.figure()
p = "Blue Colours Difference"
p1 = "Blue"
plt.title(f"{p}")
setdpi = 300
subList = [element for element in light_list if p1 in element.name]
for x in subList:
    print(x.name, x.LB, x.RB)
    
x = np.array([str(x.name) for x in subList])
LB = np.array([x.LB for x in subList])
RB = np.array([x.RB for x in subList])
colour = np.array([x.colour for x in subList])


plt.barh(x, RB-LB, color = colour, left = LB, xerr = 1, capsize = 2)

plt.xlabel("Wavelength "+ (r'$\lambda$')+ " [nm]")
plt.ylabel("Types of light")
plt.xlim(min(LB)-5,max(RB)+5)

plt.grid(True, axis = "x")
plt.savefig(f'pictures/{p}_diffraction_graph.png', dpi=setdpi)

plt.show()

我在文档中查找了它并尝试查找有关该问题的在线资源

python matplotlib
1个回答
0
投票

当您在

xerr
中使用
barh
关键字时,它只是将误差线的绘制传递给
errorbar
函数。因此,为了更好地控制误差条定位,只需从
xerr
中删除
barh
并在绘制条形后直接使用
errorbar
,例如,

from matplotlib import pyplot as plt

y = [1, 2]
width = [3, 4]
left = [1, 2]

err = 1

plt.barh(y, width, left=left)

# plot the error bars on the left
plt.errorbar(left, y, xerr=err, ls="none", marker=None, color="k", capsize=2)

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.