How to draw horizontal gridlines in Python Matplotlib?

To draw horizontal gridlines in a plot, use matplotlib.pyplot.grid() function and pass 'y' to the keyword argument axis.

import matplotlib.pyplot as plt

x = [1,2,3,4,5,6]
y = [1,2,3,4,5,6]

plt.plot(x, y)
plt.grid(axis='y')

Output

Matplotlib Horizontal Gridlines

Note: If you want to draw both vertical and horizontal gridlines, then simply call plt.grid() without any arguments.

import matplotlib.pyplot as plt

x = [1,2,3,4,5,6]
y = [1,2,3,4,5,6]

plt.plot(x, y)
plt.grid()

Output

Matplotlib Gridlines