操纵网格 matplotlib 中的方块数

Manipulate number of squares in a grid matplotlib

现在,我使用选项

在我的地块中创建了一个网格
from matplotlib import pyplot as plt
plt.grid(True)

由于我的绘图的性质,网格线在 x 方向上每 500 个单位,在 y 方向上每 5 个单位。有没有一种方法可以增加水平线的数量(即每 y 单位增加一条线)?

您可以使用 which='minor' 执行此操作,但您需要先打开次要刻度。例如:

import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0,10,100)
y = x**2
plt.plot(x,y)
ax = plt.gca()
minor_ticks = np.arange(0,100,5)
ax.set_yticks(minor_ticks, minor=True)
ax.yaxis.grid(which='minor')
plt.show()