python图片顶端,python-如何在条形图的顶部显示值

Python newbie here. I want to show values above each bin in the following graph:

m17ox.png

This is my code:

x=[i for i in range(1,11)]

y=[0.95,

0.95,

0.89,

0.8,

0.74,

0.65,

0.59,

0.51,

0.5,

0.48]

plt.bar(x, height= y)

xlocs, xlabs = plt.xticks()

xlocs=[i+1 for i in range(0,10)]

xlabs=[i/2 for i in range(0,10)]

plt.xlabel('Max Sigma')

plt.ylabel('Test Accuracy')

plt.xticks(xlocs, xlabs)

plt.show()

this is the graph that I want:

gGUMT.png

解决方案

Simply add

for i, v in enumerate(y):

plt.text(xlocs[i] - 0.25, v + 0.01, str(v))

before plt.show(). You can adjust the centralization or height of the text by changing the (-0.25) and (0.01) values, respectively.

biLIW.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值