2

I've plot a barplot in seaborn using the follow function:

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

Now I'd like to color each bar according to the following rule:

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)

I believe I can access the bars through ax.patches, which returns a set of rectangles:

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)

However, I don't know how to retrieve the yerr data calculated by seaborn/matplotlib.

pceccon
  • 9,379
  • 26
  • 82
  • 158

1 Answers1

3

Just like ax.patches, you can use ax.lines. Of course, I assume that error bars are the only lines you have in your plot otherwise you may have to do something extra to uniquely identify the error bars. The following works:

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