'Unable to position matplotlib ax.annotate correctly [duplicate]

in the below code, I'm trying to plot stacked bar chart of % of stoke events against the gender variable.

dfgender=df.groupby('gender')['stroke'].value_counts(normalize=True).unstack().fillna(0)
ax=dfgender.plot.bar(stacked=True,colormap='tab10',figsize=(8, 6))
plt.legend(loc="upper right", ncol=1, prop={'size': 8})
plt.title('Gender vs Stroke')
plt.xlabel("Gender")
plt.ylabel("Proportion")
for p in ax.patches:
    ax.annotate(round(p.get_height(),2), (p.get_x()+p.get_width()/2, p.get_height()),ha='center',va='center',xytext=(10, 0), textcoords='offset points')
plt.show()

enter image description here

But as you can see, the placement of the bar labels is incorrect. 1 should be 5% and 0 should be 95%. I want the figures to be represented in percentages and be positioned in the center of the bars. Can somebody help me please?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source