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

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

问题描述

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

plt.plot(range(2003,2012,1),range(200300,201200,100))# 其他问题的几个解决方案没有奏效,包括# plt.ticklabel_format(style='sci', axis='x', scilimits=(-1000000,1000000))# ax.get_xaxis().get_major_formatter().set_useOffset(False)plt.show()

解决方案

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

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

例如:

fig, ax = plt.subplots()ax.plot(范围(2003,2012,1),范围(200300,201200,100))ax.ticklabel_format(useOffset=False)plt.show()

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

<小时>

偏移量"和科学记数法"的区别

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

考虑这个例子:

将 numpy 导入为 np导入 matplotlib.pyplot 作为 pltx = np.linspace(1000, 1001, 100)y = np.linspace(1e-9, 1e9, 100)图, ax = plt.subplots()ax.plot(x, y)plt.show()

x 轴将有一个偏移量(注意 + 符号),y 轴将使用科学记数法(作为乘数 -- 无加号).

我们可以单独禁用任何一个.最方便的方法是ax.ticklabel_format 方法(或plt.ticklabel_format).

例如,如果我们调用:

ax.ticklabel_format(style='plain')

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

如果我们打电话

ax.ticklabel_format(useOffset=False)

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

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

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

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.

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

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


Difference between "offset" and "scientific notation"

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

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

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

For example, if we call:

ax.ticklabel_format(style='plain')

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

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:

Finally, we can disable both through:

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

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

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