python - 在 matplotlib 图表上设置 Y 轴值,递增

标签 python pandas matplotlib charts

请有人在这方面帮助我。我正在尝试将 Y 轴值添加到我的 matplotlib 图表中,以便图表看起来像这样的 Excel 图表:

enter image description here

但是,在我的 matplotlib 图表上,Y 轴值从 0 到 6:

enter image description here

我是否遗漏了一些代码来将值添加到图表中?我是否需要手动指定它们的值以 1000 万英镑为增量?抱歉,我对此还很陌生!

df_total_gross_sales.set_index('Date').plot.bar()

plt.title('Gross Sales: Discounted Vs Full Price', pad = 10, Fontsize =16)
plt.ylabel("Gross Sales (£)", labelpad = 10, fontsize=14)
plt.xlabel("Year-Month", labelpad = 20, fontsize=14)

plt.tight_layout()
plt.legend(facecolor='white', fontsize =13,  framealpha=1)
plt.show()

enter image description here

最佳答案

只需添加

plt.gca().yaxis.set_major_formatter('₤{x:,}')

显示之前的某个位置。

一个minimal reproducible example :

import numpy as np
import matplotlib.pyplot as plt

x=np.arange(30)
y=np.random.randint(5000000, 70000000, (30,))
plt.bar(x,y)
plt.gca().yaxis.set_major_formatter('₤{x:,}')
plt.show()

给予 enter image description here

同一示例(没有该行)显示了 y 轴,其刻度标记为 0 到 7,如您的示例所示,图例中表示这些是 ×10⁷ 单位。

请注意,此格式字符串中的刻度值始终命名为 x(即使它是 y 轴)。

关于python - 在 matplotlib 图表上设置 Y 轴值,递增,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74848812/

相关文章:

python - 在 Python 中使用列表而不是字典进行优化

python - 使用 Python matplotlib 绘制 3d 矢量

python - 使用 numpy 进行乘法累加

python - 如何使用时间点计算 Pandas 中的累积 groupby 计数?

python - 通过类方法更新全局数据框

python - Pandas 相当于 dplyr dot

python - Pandas 散点矩阵 - 直方图是什么意思?

python - 使用 Matplotlib 以非阻塞方式绘图

python - 如何避免在两个不同的函数中重复 for 循环

python - Google Colab csv 文件上传速度慢得可怜