You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pyplot.barh水平柱状图如何在区间左侧添加误差棒

解决pyplot.barh水平柱状图误差棒仅单侧显示的问题

使用plt.barh()绘制水平柱状图时,若通过left参数指定了柱状图的起始位置,设置xerr=1会导致误差棒仅在柱状图右侧(长度末端)显示。要实现左右两侧都添加相同误差值的误差棒,只需调整xerr的参数格式:

将xerr=1改为xerr=[1, 1],其中数组第一个元素对应左侧误差,第二个元素对应右侧误差,这样就能在柱状图两端都生成误差棒。

修改后的完整代码如下:

import matplotlib.pyplot as plt
import numpy as np

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

# 修改xerr参数为二维数组,分别指定左右误差
plt.barh(x, RB-LB, color=colour, left=LB, xerr=[1, 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()

内容的提问来源于stack exchange,提问作者Nechl

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 15:25:59