Cannot get minor grid lines to appear in matplotlib figure

℡╲_俬逩灬. 提交于 2019-11-29 09:12:35

Unfortunately, ax.grid is a bit confusing in this regard. (This is a design bug / common gotcha.) It turns the minor grid on, but the minor ticks are still turned off.

What you need to do is call plt.minorticks_on or ax.minorticks_on in addition to calling ax.grid(True, which='both').

stephenb

You should use plt.minorticks_on().

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure(1)
ax = fig.add_subplot(111)

x = np.linspace(0,10,41)
y = np.sin(x)

plt.plot(x,y)
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()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!