带有对数轴的 matplotlib 图,但没有科学/指数符号

aph*_*aph 4 python plot matplotlib

我正在制作一个 x 轴应以对数间隔排列的图形,但我想手动设置刻度标签,并且我希望刻度标签以普通的 '%.2f' 表示法出现,而不是指数表示法。以下基于Matplotlib 的解决方案- 对数标度,但需要非对数标签建议使用 ScalarFormatter,但不适用于 matplotlib 2.0:

x = np.logspace(2, 3, 100)
y = x

fig, ax = plt.subplots(1, 1)
xscale = ax.set_xscale('log')
ax.set_xticks((100, 200, 300, 500))
xlim = ax.set_xlim(100, 1000)

from matplotlib.ticker import ScalarFormatter
ax.get_xaxis().set_major_formatter(ScalarFormatter())

__=ax.plot(x, y)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

Imp*_*est 5

使用 aScalarFormatter肯定是可能的。然后,您需要确保没有显示任何次要刻度标签,如以下问题所示:Matplotlib: setting x-limits also force tick labels?

在您的情况下,代码将如下所示:

import matplotlib.pyplot as plt
import numpy as np

x = np.logspace(2, 3, 100)
y = x

fig, ax = plt.subplots(1, 1)
xscale = ax.set_xscale('log')
ax.set_xticks((100, 200, 300, 500))
xlim = ax.set_xlim(100, 1000)

import matplotlib.ticker

ax.get_xaxis().set_major_formatter(matplotlib.ticker.ScalarFormatter())
ax.get_xaxis().set_minor_formatter(matplotlib.ticker.NullFormatter())

__=ax.plot(x, y)

plt.show()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明