热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何在Matplotlib条形图中精确标注每个条目的数值?

如何用 Matplotlib 显示条形图中各条的数值?原文:https://www . geeksforgeeks . org/

如何用 Matplotlib 显示条形图中各条的数值?

原文:https://www . geeksforgeeks . org/如何使用-matplotlib/ 显示条形图中每个条形图的价值

在本文中,我们将看到如何使用 Matplotlib 在酒吧聊天中显示每个酒吧的值。在 matplotlib 中,有两种不同的方法来显示条形图中每个条形图的值–


  • 使用matplotlib . axes . axes . text()函数。

  • 使用 matplotlib.pyplot.text() 函数。

示例 1:使用matplotlib . axes . text()功能:

这个功能基本上是用来给图表中的位置添加一些文字。此函数返回字符串,这总是与语法“一起用于索引,value in enumerate(iterable) ”一起使用,iterable 作为访问每个索引的条值列表,value pair in iterable 因此它可以在每个条上添加文本。

Python 3

import os
import numpy as np
import matplotlib.pyplot as plt
x = [0, 1, 2, 3, 4, 5, 6, 7]
y = [160, 167, 17, 130, 120, 40, 105, 70]
fig, ax = plt.subplots()
width = 0.75
ind = np.arange(len(y))
ax.barh(ind, y, width, color = "green")
for i, v in enumerate(y):
    ax.text(v + 3, i + .25, str(v), 
            color = 'blue', fOntweight= 'bold')
plt.show()

输出:

示例 2:使用 功能:

调用 matplotlib.pyplot.barh(x,height),用 x 作为条形图名称列表,用 height 作为条形图值列表,创建条形图。使用语法“”作为 index,value in enumerate(iterable) ”并以 iterable 作为 bar 值的列表来访问 iterable 中的每个 index,value 对。在每次迭代中,调用 matplotlib.pyplot.text(x,y,s),其中 x 为值,y 为索引,s 为字符串(值),用其大小标记每个条。

Python 3

import matplotlib.pyplot as plt
x = ["A", "B", "C", "D"]
y = [1, 2, 3, 4]
plt.barh(x, y)
for index, value in enumerate(y):
    plt.text(value, index,
             str(value))
plt.show()

输出:


推荐阅读
author-avatar
天若无雨666
这个菇凉很宅,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有