如何在 Matplotlib 中禁用对数图的次要刻度?
matplotlibpythondata visualization更新于 2025/6/1 11:24:17
要禁用 matplotlib 中对数图的次要刻度,我们可以使用 minorticks_off() 方法。
步骤
- 设置图形大小并调整子图之间和周围的填充。
- 使用 numpy 创建 x 和 y 数据点。
- 在索引 1 处向当前图形添加子图。
- 使用 color=red 绘制 x 和 y 数据点。
- 按名称将 x 比例设为对数类。
- 设置当前图的标题。
- 在索引 2 处向当前图形添加子图。
- 使用color=green。
- 按名称将 x-scale 设为对数类。
- 关闭绘图的次要刻度。
- 将绘图的标题设置为索引 2。
- 要显示图形,请使用 show() 方法。
示例
import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
x = np.random.randint(-3, 3, 10)
y = np.exp(x)
plt.subplot(121)
plt.plot(y, x, c='red')
plt.xscale('log')
plt.title("With minor ticks")
plt.subplot(122)
plt.plot(y, x, c='green')
plt.xscale('log')
plt.minorticks_off()
plt.title("Without minor ticks")
plt.show()
输出

相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

