python直方图如何显示值?

2025-12-02 0 7,278

python直方图显示值的方法:

可以使用text()函数通过为直方图添加注释的方法来显示值。

函数功能:添加图形内容细节的无指向型注释文本

函数语法:

plt.text(x, y, string, weight="bold", color="b")

参数说明:

  • x: 注释文本内容所在位置的横坐标

  • y:注释文本内容所在位置的纵坐标

  • string:注释文本内容

  • weight:注释文本内容的粗细风格

  • color:注释文本内容的字体颜色

# -*- coding: utf-8 -*-

import matplotlib.pyplot as plt
 
#num_list = [1.5,0.6,7.8,6]
fig, ax = plt.subplots()
num_list = [7870,81937,17529598,6225227]
name_list = ['Eviction','Failed','Finished','Killed']
b = ax.bar(name_list, num_list)

plt.bar(range(len(num_list)), num_list,color='rbg',tick_label=name_list)

for a, b in zip(name_list, num_list):
    ax.text(a, b+1, b, ha='center', va='bottom')

plt.title('Numbers of Four eventtypes')
plt.xlabel('Eventtype')
plt.ylabel('Number')
plt.show()

效果如下:

更多Python知识请关注Python自学网。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:以上部本文内容由互联网用户自发贡献,本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。投诉邮箱:3758217903@qq.com

ZhiUp资源网 常见问题 python直方图如何显示值? https://www.zhiup.top/6880.html

相关