我使用以下函数在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)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)