【发布时间】:2022-09-25 00:55:05
【问题描述】:
我有这个数据框
rules count percentage groups weight
A 15 24% 1 10
B 5 2% 2 30
C 25 50% 3 50
我有以下代码:
sns.set(rc={\'figure.figsize\':(18,9.5)})
plots = sns.barplot(x=\"rules\", y=\"count\", data=df, hue=df[\'groups\'], dodge=False)
percentage = df[\'percentage\'].tolist()
weight = df[\'weight\'].tolist()
patches = plots.patches
for i in range(len(patches)):
x = patches[i].get_x() + patches[i].get_width()/2
y = patches[i].get_height()+.09
plots.annotate(\'{:.1f}%\'.format(percentage[i]), (x, y), ha=\'center\', va=\'bottom\', size=14)
plots.annotate(\'{:.0f}\'.format(weight[i]), (x, y), ha=\'center\', va=\'top\', color=\'white\', size=15, fontweight=\"bold\")
尝试用百分比注释条形图时出现以下错误。
条形图内部是另一个数字,对应于 df 中的权重列。
IndexError Traceback (most recent call last)
<ipython-input-120-0ef14f891711> in <module>()
7 x = patches[i].get_x() + patches[i].get_width()/2
8 y = patches[i].get_height()+.09
----> 9 plots.annotate(\'{:.1f}%\'.format(percentage[i]), (x, y), ha=\'center\', va=\'bottom\', size=14)
10 plots.annotate(\'{:.0f}\'.format(weight[i]), (x, y), ha=\'center\', va=\'top\', color=\'white\', size=15, fontweight=\"bold\")
11 plots.set_xticklabels(plots.get_xticklabels(), rotation=90, fontsize=15)
IndexError: list index out of range
标签: python matplotlib seaborn bar-chart plot-annotations