繁体   English   中英

"如何在 matplotlib 对数图上删除科学记数法"

[英]How to remove scientific notation on a matplotlib log-log plot

我知道以前有人问过这个问题,但是我尝试了所有可能的解决方案,但没有一个对我有用。

所以,我在 matplotlib 中有一个对数图,我想避免在 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()

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

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

以下可以用作解决方法(原始答案<\/a>):

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

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

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

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

"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM