0

I am having difficult trying to get minor gridlines to appear on a plot. I have seen a couple of SO questions on this, this one suggesting two grid objects (one for major and one for minor) and this one suggesting also adding minorticks_on.

For some reason the minor gridlines are still not appearing when I use the code below, can anyone shed any light please:

from __future__ import division
from matplotlib import pyplot as plt
from math import log, sqrt

x = range(1, 20)
x_lin = x
x_log = [log(i) for i in x]

x2 = [sqrt(i) for i in x]
x2_exp = x

ax = plt.subplot(1,1,1)
p1 = ax.plot(x, x_lin, 'g--', label='linear', linewidth=2)
p2 = ax.plot(x, x_log, 'b-', label='log', linewidth=3)
p3 = ax.plot(x2, x2_exp, 'r-', label='exp', linewidth=3)
l1 = ax.legend()
g1 = ax.grid(b=True, which='both', color='k', linestyle='-')
g2 = ax.grid(b=True, which='minor', color='k', linestyle='--')
ax.minorticks_on
plt.show()

Here is what I am getting:

enter image description here

Community
  • 1
  • 1
ChrisProsser
  • 12,598
  • 6
  • 35
  • 44

2 Answers2

4

Following the answer given by @M4rtini here is the corrected code and new image:

from __future__ import division
from matplotlib import pyplot as plt
from math import log, sqrt

x = range(1, 20)
x_lin = x
x_log = [log(i) for i in x]

x2 = [sqrt(i) for i in x]
x2_exp = x

ax = plt.subplot(1,1,1)
p1 = ax.plot(x, x_lin, 'g--', label='linear', linewidth=2)
p2 = ax.plot(x, x_log, 'b-', label='log', linewidth=3)
p3 = ax.plot(x2, x2_exp, 'r-', label='exp', linewidth=3)
l1 = ax.legend()
g1 = ax.grid(b=True, which='major', color='k', linestyle='-', linewidth=0.5)
g2 = ax.grid(b=True, which='minor', color='k', linestyle='-', linewidth=0.2)
ax.minorticks_on()
plt.show()

ChrisProsser
  • 12,598
  • 6
  • 35
  • 44
2

change: ax.minorticks_on to ax.minorticks_on()

M4rtini
  • 13,186
  • 4
  • 35
  • 42