如何在seaborn中检索错误栏

pce*_*con 3 python matplotlib seaborn

我使用以下函数在seaborn中绘制了条形图:

ax = sns.barplot(x='Year', y='Value', data=df)
Run Code Online (Sandbox Code Playgroud)

现在我想根据以下规则为每个条形着色:

percentages = []
for bar, yerr_ in zip(bars, yerr):
    low  = bar.get_height() - yerr_
    high = bar.get_height() + yerr_
    percentage = (high-threshold)/(high-low)
    if percentage>1: percentage = 1
    if percentage<0: percentage = 0
    percentages.append(percentage)
Run Code Online (Sandbox Code Playgroud)

我相信我可以通过 ax.patches 访问这些条,它返回一组矩形:

for p in ax.patches:
    height = p.get_height()
    print(p)
>> Rectangle(-0.4,0;0.8x33312.1)
>> Rectangle(0.6,0;0.8x41861.9)
>> Rectangle(1.6,0;0.8x39493.3)
>> Rectangle(2.6,0;0.8x47743.6)
Run Code Online (Sandbox Code Playgroud)

但是,我不知道如何检索由seaborn/matplotlib计算的yerr数据。

小智 5

就像ax.patches,你可以使用ax.lines。当然,我假设误差线是图中唯一的线,否则你可能需要做一些额外的事情来唯一标识误差线。以下作品:

    for p in ax.lines:
        width = p.get_linewidth()
        xy = p.get_xydata()
        print(xy)
        print(width)
        print(p)
Run Code Online (Sandbox Code Playgroud)