关于python:在matplotlib.pyplot中防止科学计数法

prevent scientific notation in matplotlib.pyplot

本问题已经有最佳答案,请猛点这里访问。

我已经试图抑制pyplot中的科学计数法了几个小时。 在尝试多种解决方案均未成功后,我需要一些帮助。

1
2
3
4
5
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()

plot


就您而言,您实际上是想禁用偏移量。使用科学计数法是与以偏移值表示事物不同的设置。

但是,ax.ticklabel_format(useOffset=False)应该可以工作(尽管您将其列为无效的事情之一)。

例如:

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

enter image description here

如果要同时禁用偏移量和科学概念,则可以使用ax.ticklabel_format(useOffset=False, style='plain')

"偏移量"和"科学计数法"之间的区别

在matplotlib轴格式中,"科学计数法"是指所显示数字的倍数,而"偏移量"是单独添加的术语。

考虑以下示例:

1
2
3
4
5
6
7
8
9
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轴将使用科学计数法(作为乘数-无加号)。

enter image description here

我们可以分别禁用其中之一。最方便的方法是ax.ticklabel_format方法(或plt.ticklabel_format)。

例如,如果我们调用:

1
ax.ticklabel_format(style='plain')

我们将在y轴上禁用科学计数法:

enter image description here

如果我们打电话

1
ax.ticklabel_format(useOffset=False)

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

enter image description here

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

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

enter image description here