如何在python的条形图中添加百分比

时间:2019-03-14 18:02:15

标签: python matplotlib

此代码为我提供了条形图,但我也想插入百分比,我将如何处理?

Hipertension_factor = appointments.groupby(['No-show'])['Hipertension'].count()
Hipertension_factor.plot(kind = 'bar', title = 'Hipertension')
plt.xlabel('No-show', fontsize = 18)
plt.ylabel('Hipertension', fontsize = 18)

1 个答案:

答案 0 :(得分:0)

我们无法提供代码来解决您的问题,但是有很多方法可以在matplotlib中注释图表(我将假定您正在使用),如下所示

https://matplotlib.org/gallery/text_labels_and_annotations/annotation_demo.html#sphx-glr-gallery-text-labels-and-annotations-annotation-demo-py

其中的一个基本示例是:

ax.annotate('straight',
    xy=(0, 1), xycoords='data',
    xytext=(-50, 30), textcoords='offset points',
    arrowprops=dict(arrowstyle="->"))

您只需要使用标准count or what you want / total count of possibilities公式计算百分比,然后就可以使用与创建条形图相同的x,y绘制结果并包括一个包含您刚刚计算的百分比的字符串(在上面的示例中替换为“直线”)。这个问题也没有直接的答案,因为您可能想在条形图上稍加注释,这与在条形图上精确绘制有所不同...