gpt4 book ai didi

python - 防止科学记数法

转载 作者:IT老高 更新时间:2023-10-28 21:44:42 25 4
gpt4 key购买 nike

我已经尝试在 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()

plot

Is ticklabel_format broken?并不能解决实际移除偏移量的问题。

plt.plot(np.arange(1e6, 3 * 1e7, 1e6))
plt.ticklabel_format(useOffset=False)

enter image description here

最佳答案

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

但是,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()

enter image description here

如果您想同时禁用偏移量和科学记数法,您可以使用 ax.ticklabel_format(useOffset=False, style='plain')


“偏移”和“科学记数法”的区别

在 matplotlib 轴格式中,“科学记数法”指的是数字显示的乘数,而“偏移量”是一个单独的术语,添加。 p>

考虑这个例子:

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

例如,如果我们调用:

ax.ticklabel_format(style='plain')

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

enter image description here

如果我们调用

ax.ticklabel_format(useOffset=False)

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

enter image description here

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

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

enter image description here

关于python - 防止科学记数法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28371674/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com