在这种情况下,如何使用 barh() 在条形图上显示值? [复制]

Posted

技术标签:

【中文标题】在这种情况下,如何使用 barh() 在条形图上显示值? [复制]【英文标题】:How to display the values on the bar plot for each bar with barh() in this case? [duplicate] 【发布时间】:2021-10-29 00:52:22 【问题描述】:

在这种情况下如何在它的顶部添加bar的值?

我想得到什么:

【问题讨论】:

【参考方案1】:

您应该添加为matplotlib.axes.Axes.text。 如果你有这样的情节:

import matplotlib.pyplot as plt


labels = ['A', 'B', 'C']
values = [150, 80, 10]


fig, ax = plt.subplots()

ax.barh(labels, values)

plt.show()

您可以使用此循环添加标签(您可能需要调整 x 轴范围以适合标签):

for i, value in enumerate(values):
    ax.text(value + 3, i, str(value))
xmin, xmax = ax.get_xlim()
ax.set_xlim(xmin, 1.1*xmax)

【讨论】:

以上是关于在这种情况下,如何使用 barh() 在条形图上显示值? [复制]的主要内容,如果未能解决你的问题,请参考以下文章

使用 ggplot 在分组条形图上定位标签

如何在条形图上更正 R 中 y 轴的比例和顺序

matplotlib barh 图上的圆点

如何使用 mplcursors 在条形图上添加悬停注释

matplotlib可视化篇barh()--直方图(2)

来自 Pandas 数据框的 Matplotlib 中堆积条形图中的标签问题