如何在Matplotlib中设置次要刻度的位置?
要在Matplotlib中设置次要刻度的位置,可以采取以下步骤 –
- 设置图形大小并调整子图之间及周围的填充。
- 使用numpy创建 x 和 y 数据点。
- 创建一个图形和一组子图。
- 使用 plot() 方法绘制 x 和 y 数据点。
- 使用 set_minor_locator() 方法来定位次要刻度。
- 使用 grid(which =’minor’) 显示次要刻度。
- 使用 show() 方法显示图形。
示例
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import AutoMinorLocator
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
x = np.linspace(1, 10, 100)
y = np.log(x)
fig, ax = plt.subplots()
ax.plot(x, y)
minor_locator = AutoMinorLocator(2)
ax.xaxis.set_minor_locator(minor_locator)
plt.grid(which='minor')
plt.show()