为什么 LogLocator 每 2 个十年只给我一次滴答?
Why does LogLocator give me only one tick per 2 decades?
首先,代码。我正在使用 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
,我只会在 even 的 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()
首先,代码。我正在使用 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
,我只会在 even 的 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()