gpt4 book ai didi

python - 防止科学记数法

转载 作者:太空宇宙 更新时间:2023-11-03 20:55:30 25 4
gpt4 key购买 nike

我有以下代码:

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/56060817/

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