Search code examples
pythonmatplotlibplotsubplot

Matplotlib won't show minor ticks when using subplots


I have several subplots and want to adjust the axis ticks settings by ax.tick_params. Everything works fine, however, the minor ticks are not shown. Here is a code example

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()

I assumed that which=both would give me the minor ticks. However I need to add an additional

plt.minorticks_on()

which makes them visible but only in ax2.

How do I fix this?


Solution

  • With pyplot the danger is that you loose track of which one the current axes is that a command like plt.minorticks_on() operates on. Hence it would be beneficial to use the respective methods of the axes you're working with:

    ax1.minorticks_on()
    ax2.minorticks_on()