我使用以下功能绘制了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数据。
答案 0 :(得分:1)
就像ax.patches
一样,您可以使用ax.lines
。当然,我假设误差线是你的图中唯一的线,否则你可能需要做一些额外的事情来唯一地识别误差线。以下作品:
for p in ax.lines:
width = p.get_linewidth()
xy = p.get_xydata()
print(xy)
print(width)
print(p)