Set minor ticks in all log-log subplots

426 Views Asked by At

I have a figure with two subplots in log-log scale. I would like to plot the minor ticks as well. Even though I have applied different solutions from Stack Overflow, my figure does not look as I want.

One of the solutions I have modified comes from ImportanceOfBeingErnest and the code looks like this:

fig, ((ax1, ax2)) = plt.subplots(1, 2, figsize=(8, 5), sharey=True)

# First plot
ax1.loglog(PLOT1['X'], PLOT1['Y'], 'o',
         markerfacecolor='red', markeredgecolor='red', markeredgewidth=1,
         markersize=1.5, alpha=0.2)

ax1.set(xlim=(1e-4, 1e4), ylim=(1e-8, 1e2))
ax1.set_xscale("log"); ax1.set_yscale("log")
ax1.xaxis.set_major_locator(matplotlib.ticker.LogLocator(base=10.0, numticks=25))
ax1.yaxis.set_major_locator(matplotlib.ticker.LogLocator(base=10.0, numticks=25))

locmaj = matplotlib.ticker.LogLocator(base=10,numticks=25) 
ax1.xaxis.set_major_locator(locmaj)
locmin = matplotlib.ticker.LogLocator(base=10.0,subs=(0.2,0.4,0.6,0.8),numticks=25)
ax1.xaxis.set_minor_locator(locmin)
ax1.xaxis.set_minor_formatter(matplotlib.ticker.NullFormatter())

locmaj = matplotlib.ticker.LogLocator(base=10,numticks=25) 
ax1.yaxis.set_major_locator(locmaj)
locmin = matplotlib.ticker.LogLocator(base=10.0,subs=(0.2,0.4,0.6,0.8),numticks=25)
ax1.yaxis.set_minor_locator(locmin)
ax1.yaxis.set_minor_formatter(matplotlib.ticker.NullFormatter())

ax1.set_xlabel('X values', fontsize=10, fontweight='bold')
ax1.set_ylabel('Y values', fontsize=10, fontweight='bold')

# Plot 2
ax2.loglog(PLOT2['X'], PLOT2['Y'], 'o',
         markerfacecolor='blue', markeredgecolor='blue', markeredgewidth=1,
         markersize=1.5, alpha=0.2)

ax2.set(xlim=(1e-4, 1e4), ylim=(1e-8, 1e2))
ax2.xaxis.set_major_locator(matplotlib.ticker.LogLocator(base=10.0, numticks=25))
ax2.yaxis.set_major_locator(matplotlib.ticker.LogLocator(base=10.0, numticks=25))

locmaj = matplotlib.ticker.LogLocator(base=10,numticks=25) 
ax2.xaxis.set_major_locator(locmaj)
ax2.yaxis.set_major_locator(locmaj)

locmin = matplotlib.ticker.LogLocator(base=10.0,subs=(0.2,0.4,0.6,0.8),numticks=25)
ax2.xaxis.set_minor_locator(locmin)
ax2.yaxis.set_minor_locator(locmin)

ax2.xaxis.set_minor_formatter(matplotlib.ticker.NullFormatter())
ax2.yaxis.set_minor_formatter(matplotlib.ticker.NullFormatter())

ax2.set_xlabel('X values', fontsize=10, fontweight='bold')
ax2.set_ylabel('Y values', fontsize=10, fontweight='bold')
ax2.minorticks_on()

plt.show()

The plot I get is the following. As you can see, the minor ticks only appear on the x-axis from ax1.

Resulting plot with missing minor ticks

How can I set the minor ticks in both subplots and both axis (x and y)?

Thank you so much.

0

There are 0 best solutions below