【发布时间】:2019-12-09 14:14:12
【问题描述】:
我希望在 catplot 中的条形顶部添加一个 % 值。我尝试使用此answer 中的以下代码。但我收到一条错误消息:
a=df.groupby([ var,Gender,strata]).agg({'one':'count'}).reset_index()
total = float(len(a))
sns.set_palette("Set2")
sns.set_style("whitegrid")
sns.despine()
g = sns.catplot(x="one", y=var,hue= Gender, col=strata,
data=a, kind="bar",
height=6, aspect=1);
# g.fig.suptitle(var) # if we want to add the title
g.set_axis_labels(x_var="")
for p in g.patches:
height = p.get_height()
ax.text(p.get_x()+p.get_width()/2.,
height + 3,
'{:1.2f}'.format(height/total),
ha="center")
错误信息如下
'FacetGrid' object has no attribute 'patches'
【问题讨论】: