如何删除matplotlib log-log图上的科学记数法

时间:2018-04-10 09:30:01

标签: python matplotlib plot notation

我知道之前已经问过这个问题,但我尝试了所有可能的解决方案,但没有一个能为我工作。

所以,我在matplotlib中有一个log-log图,我想避免x轴上的科学记数法。

这是我的代码:

from numpy import array, log, pi
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import matplotlib.ticker as mticker

plt.rc('axes.formatter', useoffset=False)

tc = array([7499680.0, 12508380.0, 23858280.0, 34877020.0, 53970660.0, 89248580.0, 161032860.0, 326814160.0, 784460200.0])

theta = array([70, 60, 50, 45, 40, 35, 30, 25, 20])

plt.scatter(theta,tc)

ax=plt.gca()

ax.set_xscale('log')
ax.set_yscale('log')

ax.xaxis.set_major_formatter(mticker.ScalarFormatter())
ax.xaxis.get_major_formatter().set_scientific(False)
ax.xaxis.get_major_formatter().set_useOffset(False)

plt.show()

这是输出: Output

正如您所看到的,x轴上的数字仍然是科学记数法。我希望将它们显示为20,30,40 ...我尝试了所有可能的解决方案而没有结果。

非常感谢所有有帮助的人。

NB。我不能使用plt.loglog()命令,因为我正在对数据做一些曲线拟合,我需要它。

NB2。我注意到发生了一件非常奇怪的事情:如果我将代码更改为yaxis.get_mayor_formatter()...,它就可以在y轴上运行!它只是在x上,它不起作用。怎么可能?

编辑:也许不清楚,但如果你看一下代码,有3种方法会影响x-tick的显示:plt.rc('axes.formatter', useoffset=False)ax.xaxis.set_major_formatter(mticker.ScalarFormatter())和{{1} }。根据我发现的情况,它们是3种方法,应该都可以单独使用,但它们没有。当然,我也一个接一个地尝试过,而不是一起尝试。

4 个答案:

答案 0 :(得分:4)

那些是x轴上的小刻度(即它们不是10的整数幂),而不是主要刻度。 matplotlib会自动确定是否应标记主要或次要标记 - 在这种情况下,因为您没有在x范围内显示任何主要标记,次标记被标记)。因此,您需要使用set_minor_formatter方法:

ax.xaxis.set_minor_formatter(mticker.ScalarFormatter())

enter image description here

它在y轴上工作的原因是因为这些刻度是主要刻度(即整数倍为10),而不是次要刻度。

答案 1 :(得分:1)

以下可用作解决方法(original answer):

from matplotlib.ticker import StrMethodFormatter, NullFormatter
ax.yaxis.set_major_formatter(StrMethodFormatter('{x:.0f}'))
ax.yaxis.set_minor_formatter(NullFormatter())

答案 2 :(得分:0)

如果要同时禁用偏移量和科学值,请使用ax.ticklabel_format(useOffset=False, style='plain')

答案 3 :(得分:0)

如果您只想将 xaxis 设置为不再使用科学记数法,您需要更改 fromatter,然后您可以将其设置为普通。

ax.xaxis.set_minor_formatter(mticker.ScalarFormatter())
ax.ticklabel_format(style='plain', axis='x')