防止 matplotlib.pyplot 中的科学记数法

prevent scientific notation in matplotlib.pyplot

几个小时以来,我一直试图在 pyplot 中抑制科学记数法。在尝试了多种解决方案后都没有成功,我希望得到一些帮助。

plt.plot(range(2003,2012,1),range(200300,201200,100))
# several solutions from other questions have not worked, including
# plt.ticklabel_format(style='sci', axis='x', scilimits=(-1000000,1000000))
# ax.get_xaxis().get_major_formatter().set_useOffset(False)
plt.show()

在您的情况下,您实际上是想禁用偏移。使用科学记数法与根据偏移值显示事物是不同的设置。

但是,ax.ticklabel_format(useOffset=False) 应该有效(尽管您已将其列为无效的方法之一)。

例如:

fig, ax = plt.subplots()
ax.plot(range(2003,2012,1),range(200300,201200,100))
ax.ticklabel_format(useOffset=False)
plt.show()

如果你想禁用偏移量和科学记数法,你会使用ax.ticklabel_format(useOffset=False, style='plain')


"offset" 和 "scientific notation" 之间的区别

在 matplotlib 轴格式中,"scientific notation" 指的是数字显示的 乘数 ,而 "offset" 是一个单独的术语,即 已添加.

考虑这个例子:

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(1000, 1001, 100)
y = np.linspace(1e-9, 1e9, 100)

fig, ax = plt.subplots()
ax.plot(x, y)
plt.show()

x 轴将有一个偏移量(注意 + 符号),y 轴将使用科学记数法(作为乘数 -- 没有加号)。

我们可以单独禁用任何一个。最方便的方法是 ax.ticklabel_format 方法(或 plt.ticklabel_format)。

例如,如果我们调用:

ax.ticklabel_format(style='plain')

我们将禁用 y 轴上的科学记数法:

如果我们调用

ax.ticklabel_format(useOffset=False)

我们将禁用 x 轴上的偏移,但保持 y 轴科学计数法不变:

最后,我们可以通过以下方式禁用两者:

ax.ticklabel_format(useOffset=False, style='plain')