Python 防止 matplotlib.pyplot 中的科学记数法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28371674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
prevent scientific notation in matplotlib.pyplot
提问by Travis Osterman
I've been trying to suppress scientific notation in pyplot for a few hours now. After trying multiple solutions without success, I would like some help.
几个小时以来,我一直试图在 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()
Thanks in advance.
提前致谢。
采纳答案by Joe Kington
In your case, you're actually wanting to disable the offset. Using scientific notation is a separate setting from showing things in terms of an offset value.
在您的情况下,您实际上想要禁用偏移量。使用科学记数法与根据偏移值显示事物是不同的设置。
However, ax.ticklabel_format(useOffset=False)
should have worked (though you've listed it as one of the things that didn't).
但是,ax.ticklabel_format(useOffset=False)
应该有效(尽管您已将其列为无效的事情之一)。
For example:
例如:
fig, ax = plt.subplots()
ax.plot(range(2003,2012,1),range(200300,201200,100))
ax.ticklabel_format(useOffset=False)
plt.show()
If you want to disable both the offset and scientific notaion, you'd use ax.ticklabel_format(useOffset=False, style='plain')
.
如果您想同时禁用偏移量和科学记数法,您可以使用ax.ticklabel_format(useOffset=False, style='plain')
.
Difference between "offset" and "scientific notation"
“偏移量”和“科学记数法”之间的区别
In matplotlib axis formatting, "scientific notation" refers to a multiplierfor the numbers show, while the "offset" is a separate term that is added.
在 matplotlib 轴格式中,“科学记数法”是指数字显示的乘数,而“偏移量”是一个单独的术语,添加.
Consider this example:
考虑这个例子:
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()
The x-axis will have an offset (note the +
sign) and the y-axis will use scientific notation (as a multiplier -- No plus sign).
x 轴将有一个偏移量(注意+
符号),y 轴将使用科学记数法(作为乘数——无加号)。
We can disable either one separately. The most convenient way is the ax.ticklabel_format
method (or plt.ticklabel_format
).
我们可以单独禁用任何一个。最方便的方法是ax.ticklabel_format
方法(或plt.ticklabel_format
)。
For example, if we call:
例如,如果我们调用:
ax.ticklabel_format(style='plain')
We'll disable the scientific notation on the y-axis:
我们将禁用 y 轴上的科学记数法:
And if we call
如果我们打电话
ax.ticklabel_format(useOffset=False)
We'll disable the offset on the x-axis, but leave the y-axis scientific notation untouched:
我们将禁用 x 轴上的偏移量,但保持 y 轴科学记数法不变:
Finally, we can disable both through:
最后,我们可以通过以下方式禁用两者:
ax.ticklabel_format(useOffset=False, style='plain')