python百分比堆积条形图_python – 在matplotlib中显示条形图上方的百分比

以下是pandas数据框和从中生成的条形图:

colors_list = ['#5cb85c','#5bc0de','#d9534f']

result.plot(kind='bar',figsize=(15,4),width = 0.8,color = colors_list,edgecolor=None)

plt.legend(labels=result.columns,fontsize= 14)

plt.title("Percentage of Respondents' Interest in Data Science Areas",fontsize= 16)

plt.xticks(fontsize=14)

for spine in plt.gca().spines.values():

spine.set_visible(False)

plt.yticks([])

我需要在相应的栏上方显示相应主题的每个兴趣类别的百分比.我可以创建一个包含百分比的列表,但我不明白如何将其添加到相应的栏上.

解决方法:

试试这种方式:

colors_list = ['#5cb85c','#5bc0de','#d9534f']

# Change this line to plot percentages instead of absolute values

ax = (result.div(result.sum(1), axis=0)).plot(kind='bar',figsize=(15,4),width = 0.8,color = colors_list,edgecolor=None)

plt.legend(labels=result.columns,fontsize= 14)

plt.title("Percentage of Respondents' Interest in Data Science Areas",fontsize= 16)

plt.xticks(fontsize=14)

for spine in plt.gca().spines.values():

spine.set_visible(False)

plt.yticks([])

# Add this loop to add the annotations

for p in ax.patches:

width, height = p.get_width(), p.get_height()

x, y = p.get_xy()

ax.annotate('{:.0%}'.format(height), (x, y + height + 0.01))

c08fG.png

标签:python,pandas,matplotlib

来源: https://codeday.me/bug/20190925/1815992.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值