【问题标题】:Labeling horizontal bar using matplotlib bar_label [duplicate]使用matplotlib bar_label标记水平条[重复]
【发布时间】:2022-07-27 00:59:49
【问题描述】:

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

我已阅读文档https://matplotlib.org/stable/gallery/lines_bars_and_markers/bar_label_demo.html,但没有帮助。请帮忙。

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

【问题讨论】:

  • bars = plt.barh(...)plt.bar_label(bars)?
  • 模块'matplotlib.pyplot'没有属性'bar_label'
  • 嗯,这是一个新功能,你需要一个最新的matplotlib版本

标签: python matplotlib bar-chart


【解决方案1】:

问题在于 bar_label 是一个新功能,可在 matplotlib 3.4 版或更高版本中使用 - 请参阅此 link。使用检查 matplotlib 的版本

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

升级到v3.4 或使用pip install matplotlib --upgrade 的最新版本。您需要在您提供的链接中包含代码,并且如 JohanC 所述,并且能够看到条形标签

【讨论】:

    猜你喜欢
    • 2021-11-22
    • 2016-07-14
    • 2016-10-13
    • 2022-01-16
    • 2020-09-17
    • 2018-07-22
    • 1970-01-01
    • 2018-09-24
    • 1970-01-01
    相关资源
    最近更新 更多