Skip to main content
 首页 » 编程设计

python之如何在 seaborn 中检索错误栏

2024年10月01日9JustinYoung

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

ax = sns.barplot(x='Year', y='Value', data=df) 

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

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) 

我相信我可以通过 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) 

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

请您参考如下方法:

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

    for p in ax.lines: 
        width = p.get_linewidth() 
        xy = p.get_xydata() 
        print(xy) 
        print(width) 
        print(p)