Python没有';t使用子批次时,不允许我更改轴刻度的数量

Python没有';t使用子批次时,不允许我更改轴刻度的数量,python,matplotlib,plot,Python,Matplotlib,Plot,我有如下代码: plt.figure() plt.subplot(211) plt.plot(timestamps_SW, np.random.randn(len(testLabelsSW_ds)), label='truth') plt.plot(timestamps_SW, np.random.randn(len(testLabelsSW_ds)), label='pred.') plt.locator_params(axis='y', nbins=5) plt.legend() plt

我有如下代码:

plt.figure()

plt.subplot(211)
plt.plot(timestamps_SW, np.random.randn(len(testLabelsSW_ds)), label='truth')
plt.plot(timestamps_SW, np.random.randn(len(testLabelsSW_ds)), label='pred.')
plt.locator_params(axis='y', nbins=5)
plt.legend()

plt.subplot(212)
plt.plot(timestamps_SE,np.random.randn(len(testLabelsSE_ds)), label='truth')
plt.plot(timestamps_SE,np.random.randn(len(testLabelsSE_ds)), label='pred.')
plt.locator_params(axis='y', nbins=5)
plt.legend()
生成的绘图如下所示:


很明显,每个y轴没有5个刻度。如何修复?

使用
plt.locator\u参数(…,nbins=5)
您要求默认定位器使用5个箱子。默认定位器是一个
自动定位器。它是
MaxNLocator
的一个子类。MaxN意味着,它将尝试找到最多
N
个好位置,其中
N
等于
nbins+1
。 “好”的位置意味着,例如,像1.0、0.25等被视为“好”,而像0.761这样的位置当然不太好

当然,两个约束条件(a)“nice”和(b)“N个位置”几乎不能同时满足。因此,“好”在这里有优先权

如果省略约束(a),只需手动放置记号即可获得
N
位置

plt.yticks(np.linspace(*plt.ylim(), 5))