python-如何在条形图的顶部显示值 [英] python - how to show values on top of bar plot

查看:43
本文介绍了python-如何在条形图的顶部显示值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此处是Python新手.我想在下图中显示每个 bin 上方的值:

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

这是我的代码:

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()

这是我想要的图表:

推荐答案

只需添加

for i, v in enumerate(y):
    plt.text(xlocs[i] - 0.25, v + 0.01, str(v))

plt.show() 之前.您可以通过分别更改(-0.25)和(0.01)值来调整文本的居中或高度.

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

这篇关于python-如何在条形图的顶部显示值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆