在seaborn python中对分组条形图中的文本进行注释
在使用seaborn创建的分组条形图中,我很难将文本注释到条形图。我在这里和谷歌做了一些研究,但找不到修复方法。下面是我迄今为止掌握的一些代码在seaborn python中对分组条形图中的文本进行注释,python,pandas,matplotlib,seaborn,bar-chart,Python,Pandas,Matplotlib,Seaborn,Bar Chart,在使用seaborn创建的分组条形图中,我很难将文本注释到条形图。我在这里和谷歌做了一些研究,但找不到修复方法。下面是我迄今为止掌握的一些代码 risk_df = df.groupby(['Year', 'Risk'], as_index=False).size().rename(columns= 'size':'Risk_count'}).set_index('Year') fig, ax = plt.subplots(figsize = (12, 8)) x = risk_df.ind
risk_df = df.groupby(['Year', 'Risk'], as_index=False).size().rename(columns= 'size':'Risk_count'}).set_index('Year')
fig, ax = plt.subplots(figsize = (12, 8))
x = risk_df.index
y = risk_df['Risk_count']
ax = sns.barplot(x=x,
y=y,
hue=risk_df['Risk'],
data = risk_df)
for p in ax.patches:
ax.annotate(format(p.get_height()),
(p.get_x() + p.get_width() / 2),
p.get_height(),
ha='center',
va='center',
xytext=(0, 9),
textcoords='offset points')
plt.title('Reports by risk rating', size = 18)
plt.tight_layout()
上面的代码向我显示了“TypeError:init()为参数'xytext'获取了多个值”的错误
当前的图是这样的,但我想增加每年每类风险的观察次数。首先感谢你的帮助!!
注释的坐标需要在元组中设置,因此括号被错误地绑定
for p in ax.patches:
ax.annotate(format(p.get_height()),
(p.get_x() + p.get_width() / 2, p.get_height()),
ha='center',
va='center',
xytext=(0, 9),
textcoords='offset points')
它起作用了。非常感谢你!!!所以我应该看到(p.get_x()+p.get_width()/2为'x',然后p.get_height()为'Y'表示ax.annotate.x是x轴的位置,Y是Y轴的高度,xytext()是要放置的字符串的位置。