如何将y轴设置为百万?

问题描述 投票:0回答:1

[嗨,我想知道如何将图的y轴设置为数百万,因此与其显示5e7而不是显示5e7,它在相同位置会显示50。谢谢

matplotlib plot axis ticker
1个回答
1
投票

您可以使用tick formatters显示如下所示的百万个数字

import numpy as np
import matplotlib.ticker as ticker

@ticker.FuncFormatter
def million_formatter(x, pos):
    return "%.1f M" % (x/1E6)


x = np.arange(1E7,5E7,0.5E7)
y = x
fig, ax = plt.subplots()

ax.plot(x,y)

ax.xaxis.set_major_formatter(million_formatter)
ax.yaxis.set_major_formatter(million_formatter)

ax.set_xlabel('X in millions')
ax.set_ylabel('Y in millions')

plt.xticks(rotation='45')

plt.show

这导致

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.