I have this dataframe
rules count percentage groups weight
A 15 24% 1 10
B 5 2% 2 30
C 25 50% 3 50
I have the following code:
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")
and I get the following error when trying to annotate the barchart with the percentage.
Inside the barchart is another number corresponding to the weight column in the 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