python - 为什么 LogLocator 每 2 个十年只给我一次刻度?

标签 python python-3.x matplotlib jupyter-notebook xticks

首先是代码。我正在使用 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()

enter image description here

关于python - 为什么 LogLocator 每 2 个十年只给我一次刻度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62287736/

相关文章:

python - 修改多列数据框

python - 我应该向我的 CNN 提供什么?大输入矩阵还是 10,000 个小输入矩阵?

python-3.x - 将 CSV 导入 pandas 数据框,以列表作为列

python - 将for循环内部和外部的print stmt连接成python中的一个句子

python - Matplotlib 绘制公平模具的直方图

python - Matplotlib 通过颜色或形状区分均值和中值

python - 如何根据字典列字段列表中的键值对过滤 DataFrame 行?

python - 即使我手动安装了 enchant,也无法在 Windows 上安装 pyenchant

python - 线性 curve_fit 总是产生 1 的斜率和 y 轴截距

python - Numpy Arange 生成不一致的小数点值