0

I noticed a 'strange' behaviour when running the following code:

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import (MultipleLocator, AutoMinorLocator)


freqs = np.logspace(2,4)
freqs_ext = np.logspace(2, 10)
fig, ax = plt.subplots(1,2)
ax [0].plot(freqs , freqs**2)
#ax[0].xaxis.set_minor_locator(AutoMinorLocator(5))
ax[0].grid(which='both')
#ax[0].minorticks_on()
ax[0].set_xscale( 'log')

ax[1].plot(freqs_ext,freqs_ext**2)
#ax[l].xaxis.set_minor_locator(AutoMinorLocator(5))
ax[1].grid(which='both')
#ax[1].minorticks on()
ax[1].set_xscale('log')

The output is the following:

output

I have tried more variants than I care to report, (some are commented out in the code above), but I cannot get matplotlib to draw minor gridlines for the plot on the right side, as it does for the one on the left.

I think I have understood that the "problem" lies in where the ticks are located for the second plot, which has a much larger span. They are every two decades and I believe this might be the source of the minor grid lines not displaying. I have played with xaxis.set_xticks and obtained ticks every decade, but still cannot get this to correctly produce the gridlines.

It is probably something stupid but I can't see it.

NOTE : I know that matplotlib doesn't turn the minor ticks on by default, and in this case this action is "triggered" by changing the scale to log (that's why axis.grid(which='both') actually only acts on the x axis)

Michele Ancis
  • 1,265
  • 4
  • 16
  • 29
  • Just found this answer which might cover the same issue: https://stackoverflow.com/questions/40311175/matplotlib-strange-double-decade-axis-ticks-in-log-plot?rq=1 I can't look it up right now but will do ASAP. – Michele Ancis Aug 31 '21 at 10:52
  • `ax[1].plot(freqs,freqs_ext**2)` If you change it to this, the x-axis will have ticks added as intended. It depends on the data you are using. – r-beginners Aug 31 '21 at 11:54
  • I understand what you mean, however this would plot the wrong relationship between Y and X values. – Michele Ancis Aug 31 '21 at 12:52

1 Answers1

0

OK, I have found this answer: Matplotlib: strange double-decade axis ticks in log plot

which actually shows how the issue is a design choice for matplotlib starting with v2. Answer was given in 2017 so, not the newest issue :)

The following code correctly plots the minor grids as wanted:

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import LogLocator

freqs = np.logspace(2,4)
freqs_ext = np.logspace(2, 10)
fig, ax = plt.subplots(1,2)
ax[0].plot(freqs , freqs**2)
ax[0].grid(which='both')
ax[0].set_xscale( 'log')

ax[1].plot(freqs_ext,freqs_ext**2)
ax[1].set_xscale('log')
ax[1].xaxis.set_major_locator(LogLocator(numticks=15))
ax[1].xaxis.set_minor_locator(LogLocator(numticks=15,subs=np.arange(2,10)))
ax[1].grid(which='both')

output

Michele Ancis
  • 1,265
  • 4
  • 16
  • 29
  • This is another [great answer](https://stackoverflow.com/questions/48985874/logarithmic-gridlines-for-seaborn-factorplot). For reference. – r-beginners Aug 31 '21 at 13:17