防止matplotlib.pyplot中的科学计数法 [英] prevent scientific notation in matplotlib.pyplot

查看:1512
本文介绍了防止matplotlib.pyplot中的科学计数法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几个小时以来,我一直在试图抑制pyplot中的科学计数法.在尝试了多种解决方案但没有成功之后,我需要一些帮助.

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.

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

谢谢.

推荐答案

在您的情况下,您实际上想禁用偏移量.使用科学计数法是一种独立的设置,不同于根据偏移值显示事物.

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.

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

However, ax.ticklabel_format(useOffset=False) should have worked (though you've listed it as one of the things that didn't).

例如:

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').

If you want to disable both the offset and scientific notaion, you'd use ax.ticklabel_format(useOffset=False, style='plain').

在matplotlib轴格式中,科学计数法"指的是显示数字的乘数,而偏移量"是一个单独的术语,并添加了 .

In matplotlib axis formatting, "scientific notation" refers to a multiplier for the numbers show, while the "offset" is a separate term that is added.

请考虑以下示例:

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轴将使用科学计数法(作为乘数-无加号).

The x-axis will have an offset (note the + sign) and the y-axis will use scientific notation (as a multiplier -- No plus sign).

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

We can disable either one separately. The most convenient way is the ax.ticklabel_format method (or plt.ticklabel_format).

例如,如果我们调用:

ax.ticklabel_format(style='plain')

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

We'll disable the scientific notation on the y-axis:

如果我们打电话

ax.ticklabel_format(useOffset=False)

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

We'll disable the offset on the x-axis, but leave the y-axis scientific notation untouched:

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

Finally, we can disable both through:

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

这篇关于防止matplotlib.pyplot中的科学计数法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆