简体   繁体   English

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

[英]Setting Y Axis values on matplotlib chart, incrementing

Please may someone assist me on this.请有人帮我解决这个问题。 I am trying to add Y Axis values to my matplotlib chart so the chart looks like this Excel Chart:我正在尝试将 Y 轴值添加到我的 matplotlib 图表中,以便图表看起来像这样的 Excel 图表:

在此处输入图像描述

However, on my matplotlib chart, the Y Axis values are from 0-6:然而,在我的 matplotlib 图表上,Y 轴值是 0-6:

在此处输入图像描述

Is there some code I have left out here to add the values to the chart?我在这里遗漏了一些代码来将值添加到图表中吗? Do I need to manually specify the values for them to be in increments of £10 million?我是否需要手动指定它们的价值以 1000 万英镑为增量? I'm still very new to this sorry!对不起,我还是很陌生!

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

在此处输入图像描述

Just add只需添加

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

Somewhere before show . show前的某个地方。

A 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()

Gives给予在此处输入图像描述

The same example, without that line, shows a y-axis with ticks labelled from 0 to 7, with, as in your example, in legend saying that those are ×10⁷ units.同样的例子,没有那条线,显示了一个 y 轴,刻度标记为 0 到 7,在你的例子中,在图例中说这些是 ×10⁷ 单位。

Note that the ticks value in this format string is always named x (even tho it is y axis).请注意,此格式字符串中的刻度值始终命名为x (即使它是 y 轴)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM