Python Matplotlib赢得';使用子批次时不显示小刻度

Python Matplotlib赢得';使用子批次时不显示小刻度,python,matplotlib,plot,subplot,Python,Matplotlib,Plot,Subplot,我有几个子批次,希望通过ax.tick_参数调整轴刻度设置。一切正常,但未显示次刻度。下面是一个代码示例 import matplotlib.pyplot as plt x = np.linspace(0,1,100) y = x*x f, (ax1,ax2) = plt.subplots(2, 1) ax1.tick_params(axis="both", direction="in", which="both", right=False, top=True) ax2.tick_para

我有几个子批次,希望通过ax.tick_参数调整轴刻度设置。一切正常,但未显示刻度。下面是一个代码示例

import matplotlib.pyplot as plt

x = np.linspace(0,1,100)
y = x*x

f, (ax1,ax2) = plt.subplots(2, 1)

ax1.tick_params(axis="both", direction="in", which="both", right=False, top=True)
ax2.tick_params(axis="both", direction="in", which="both", right=True, top=False)

ax1.plot(x,y)
ax2.plot(x,-y)

plt.show()
我假设这两个都会给我一些小的滴答声。但是,我需要添加一个额外的

plt.minorticks_on()
这使它们在ax2中可见,但仅可见


我该如何解决这个问题

使用pyplot的危险在于,您无法跟踪当前轴中的哪一个轴,而像
plt.minorticks\u on()
这样的命令正在运行。因此,使用正在使用的轴的各自方法将是有益的:

ax1.minorticks_on()
ax2.minorticks_on()

plt
将在当前轴上工作,在您的情况下,
ax2
。一种方法是首先使用您使用的方法启用它们,然后使用
AutoMinorLocator

ax1.tick_params(axis="both", direction="in", which="both", right=False, top=True)
ax2.tick_params(axis="both", direction="in", which="both", right=True, top=False)

ax1.plot(x,y)
ax2.plot(x,-y)

for ax in [ax1, ax2]:
    ax.xaxis.set_minor_locator(AutoMinorLocator(4))
    ax.yaxis.set_minor_locator(AutoMinorLocator(4))