1

I am using a semilogy graph but I am struggling to get minor grid to turn on.

I am current using:

plt.grid(b=None, which='major', axis='both', color='k', linestyle='-', linewidth=0.5)
plt.grid(b=None, which='minor', axis='both', color='r', linestyle='-', linewidth=0.2)

but it isn't producing the minor grid only the major.

Update:

So my current code is:

    plt.semilogy(xPS[p-1]/1000, zPS[p-1])
    plt.ylim(-1000000, 1000000)
    plt.xlim(0, 250)
    plt.grid(b=True, which='major', color='k', linestyle='-')
    plt.grid(b=True, which='minor', color='r', linestyle='-', alpha=0.2)
    plt.minorticks_on()
    plt.show()

An I get an output like the image below with still not y-minor grid:

enter image description here

15002941
  • 347
  • 2
  • 8

1 Answers1

2

Try to add a minorticks_on call after the two above lines:

plt.minorticks_on()

Full code:

import matplotlib.pyplot as plt

plt.semilogy(
    [20,50,100,140,180,220,250],
    [2, 45*10, 314*10**2, 42*10**3, 475*10**3, 431*10**3, 904*10**3])
plt.ylim(1, 1000000)
plt.xlim(0, 250)
plt.grid(b=True, which='major', color='k', linestyle='-')
plt.grid(b=True, which='minor', color='r', linestyle='-', alpha=0.2)
plt.minorticks_on()
plt.show()

output

enter image description here

This is a known issue as explained in this discussion.

Alexandre B.
  • 5,387
  • 2
  • 17
  • 40
  • Hi, @Alexandre B. I tried this but its still not putting the minor axis on on the y. – 15002941 Apr 27 '20 at 11:14
  • @15002941 What's about the update ? Which Python Matplotlib version are you using ? – Alexandre B. Apr 27 '20 at 11:48
  • 2
    It's to do with the range of the y axis. If you increase to `plt.ylim(0.0001, 1000000)` then the minor gridlines are not plotted, probably in order to keep drawing speed relatively fast and to keep the plot from being overcrowded with gridlines – DavidG Apr 27 '20 at 12:05