Gridlines in Matplotlib

#Python #Matplotlib

Adding gridlines in matplotlib

In matplotlib, enabling the gridlines would draw the lines according to the major and minor ticks. However, we need some gridlines at specified locations.

Adding a single gridline at location 10:

ax.axvline(10, linestyle='--', color='k')

When a bunch of lines are required, we define an array as the gridline locations then loop through the location to add in the grridlines.

gridlineslist = [1,2,3,4,5,6]

for gridline in gridlineslist:
    ax.axvline(gridline, linestyle='--', color='k')

Published: by ;

L Ma (0001). 'Gridlines in Matplotlib', Datumorphism, 01 April. Available at:

Current Ref:

  • til/programming/