为什么没有出现小网格线

时间:2014-02-28 16:12:51

标签: python python-2.7 matplotlib

我很难尝试让小网格线出现在情节上。我已经看到了几个SO问题,this one建议两个网格对象(一个用于主要对象,一个用于辅助对象)和this one建议还添加minorticks_on

由于某种原因,当我使用下面的代码时,小网格线仍然没有出现,任何人都可以放弃任何光线:

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

这是我得到的:

enter image description here

2 个答案:

答案 0 :(得分:3)

根据@ M4rtini给出的答案,这里是更正的代码和新图像:

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

答案 1 :(得分:1)

ax.minorticks_on更改为ax.minorticks_on()