使用matplotlib bar_label标记水平条

发布于 2025-01-27 12:23:57 字数 620 浏览 1 评论 0 原文

我想用 pyplot.barh() ax.bar_label 在每个栏上显示条的值。

我已经阅读了文档但这无济于事。请帮忙。

# Prepare & sort the data
df = pd.DataFrame({"coins": ["BTC", "ETH", "BNB", "XRP", "SOL", "ADA"],
                   "dominance": [42.08, 19.38, 3.68, 1.73, 1.55, 1.51]})
df = df.sort_values(by=["dominance"])

# Plot horizontal bar graph
plt.figure(figsize=[10, 9])
plt.barh(df.coins, df.dominance)

I'd like to display the value of the bar on each bar with pyplot.barh() and ax.bar_label.

I've read the document https://matplotlib.org/stable/gallery/lines_bars_and_markers/bar_label_demo.html but it didn't help. Please help.

# Prepare & sort the data
df = pd.DataFrame({"coins": ["BTC", "ETH", "BNB", "XRP", "SOL", "ADA"],
                   "dominance": [42.08, 19.38, 3.68, 1.73, 1.55, 1.51]})
df = df.sort_values(by=["dominance"])

# Plot horizontal bar graph
plt.figure(figsize=[10, 9])
plt.barh(df.coins, df.dominance)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

信愁 2025-02-03 12:23:57

问题是 bar_label 是一项新功能,在matplotlib版本3.4或更高版本中可用 - ref this link 。使用升级到 v3.4 或使用 pip pip install matplotlib -upgrade 的最新版本检查Matplotlib的版本

import matplotlib 
print('matplotlib: {}'.format(matplotlib.__version__))

。您需要在提供的链接中以及Johanc提到的链接中包含代码,并且能够查看条标签

the issue is that the bar_label is a new feature and available in matplotlib version 3.4 or later - ref this link. Check the version of matplotlib using

import matplotlib 
print('matplotlib: {}'.format(matplotlib.__version__))

Upgrade to v3.4 or latest version using pip install matplotlib --upgrade. You will need to include the code in the link you provided and as mentioned by JohanC and be able to see bar labels

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文