【问题标题】:How to show percentage change in bar plot in Python pyplot如何在 Python pyplot 中显示条形图中的百分比变化
【发布时间】:2021-09-22 15:23:04
【问题描述】:

这是一个简单的条形图:

x = [1, 2, 3]
y = [10, 45, 23]

plt.bar(x, y)

我只想显示从一个栏到另一个栏的百分比变化。也许你可以帮忙。谢谢

【问题讨论】:

  • 你的问题还没有解决吗?

标签: python-3.x matplotlib bar-chart


【解决方案1】:

您可以使用bar_label 函数(matplotlib 3.4.2):

import matplotlib.pyplot as plt

x = [1, 2, 3]
y = [10, 45, 23]

bars = plt.bar(x, y, fc='crimson', ec='navy')
plt.bar_label(bars, [''] + [f'{(y1 - y0) / y0 * 100:+.2f}%' for y0, y1 in zip(y[:-1], y[1:])])
plt.margins(y=0.1)
plt.show()

【讨论】:

  • 这个答案有帮助吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-22
  • 2019-02-04
  • 1970-01-01
  • 2022-12-01
  • 2020-02-29
  • 2023-04-02
  • 2023-04-02
相关资源
最近更新 更多