使用子图时 Matplotlib 不会显示小刻度

Bea*_*ker 6 python plot matplotlib subplot

我有几个子图,想要通过 ax.tick_params 调整轴刻度设置。一切工作正常,但是,没有显示小刻度。这是一个代码示例

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()
Run Code Online (Sandbox Code Playgroud)

我认为 which=both 会给我小刻度。但是我需要添加一个额外的

plt.minorticks_on()
Run Code Online (Sandbox Code Playgroud)

这使得它们可见,但仅在 ax2 中可见。

我该如何解决?

M-W*_*ane 5

使用 pyplot 的危险在于,您无法追踪当前轴是像这样的命令plt.minorticks_on()所操作的轴。因此,使用您正在使用的轴的相应方法将是有益的:

ax1.minorticks_on()
ax2.minorticks_on()
Run Code Online (Sandbox Code Playgroud)