How to draw vertical gridlines in Python Matplotlib?

To draw vertical gridlines in a plot, use matplotlib.pyplot.grid() function and pass 'x' 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='x')

Output

Matplotlib Vertical 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