首先是代码。我正在使用 Jupyter Notebook 6.0.3(这可能重要也可能不重要)。
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
Nmax = 6
x = np.logspace(-1, Nmax)
y = 1/x + x
W = 8
plt.figure(figsize=(W, 4))
plt.plot(x, y)
ax = plt.gca()
ax.set_xscale('log')
ax.set_yscale('log')
ax.xaxis.set_major_locator(mpl.ticker.LogLocator())
这很好用。我在 X 轴上得到 10 次方的刻度。
但是,如果我设置 Nmax=7
,我只会在 10 的偶次方(即 0、100 等)处获得刻度。增加 W(例如 W=20
)并不重要——我在相同的刻度下得到了更大的数字。
我尝试阅读 LogLocator
的文档。他们实际上并没有解释所有的输入参数。
最佳答案
您可以再次设置 xtick,如以下代码所示:
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
Nmax = 7
x = np.logspace(-1, Nmax)
y = 1/x + x
W = 8
fig, ax = plt.subplots(1, 1, figsize = (W, 4))
ax.loglog(x, y)
locmaj = mpl.ticker.LogLocator(numticks=12)
ax.xaxis.set_major_locator(locmaj)
plt.show()
关于python - 为什么 LogLocator 每 2 个十年只给我一次刻度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62287736/