问题描述
我使用以下函数在 seaborn 中绘制了条形图:
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)
我相信我可以通过ax.patches访问条形图,它会返回一组矩形:
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)
但是,我不知道如何检索seaborn/matplotlib计算出的yerr数据.
However, I don't know how to retrieve the yerr data calculated by seaborn/matplotlib.
推荐答案
就像 ax.patches
一样,您可以使用 ax.lines
.当然,我假设误差线是您绘图中唯一的线条,否则您可能需要做一些额外的事情来唯一地识别误差线.以下工作:
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)
这篇关于如何在Seaborn中检索错误栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!