Skip to Content

Show Gridlines on Matplotlib Plots

Plots made with the matplotlib library in Python are highly customizable. In this tutorial, we will look at how to show gridlines on a matplotlib plot with the help of some examples.

How to add gridlines to a plot in matplotlib?

You can use the matplotlib.pyplot.grid() function to add gridlines to a matplotlib plot. By default, it adds the gridlines to both the x-axis and the y-axis. The following is the syntax –

import matplotlib.pyplot as plt

# plot the data
plt.scatter(x, y)

# add gridlines
plt.grid(visible=True)

This function also allows you to customize the gridlines, for example, add gridlines to only a particular axis, and change the line properties such as its color, style, width, etc.

Let’s now look at some examples of using the above syntax. First, let’s create a sample plot and see if we get gridlines by default or not.

import matplotlib.pyplot as plt

# x values - years
x = [2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020]
# y values - 1 USD in INR
y = [46.67, 53.44, 56.57, 62.33, 62.97, 66.46, 67.79, 70.09, 70.39, 76.38]

# plot x and y on scatter plot
plt.scatter(x, y)

# add axes labels
plt.xlabel('Year')
plt.ylabel('1USD in INR')

Output:

scatter plot without gridlines

Example 1 – Add gridlines to a plot

Let’s now add gridlines to the above scatter plot using the matplotlib.pyplot.grid() function. Pass visible=True to show the grid lines.

# plot x and y on scatter plot
plt.scatter(x, y)

# add axes labels
plt.xlabel('Year')
plt.ylabel('1USD in INR')

# add gridlines
plt.grid(visible=True)

Output:

scatter plot with gridlines

We get the scatter plot with the gridlines. Note that if you do not pass any arguments to plt.grid(), it will also show the gridlines (in that case, the plt.grid() just toggles the visibility of the gridlines).

Example 2 – Add gridlines to a specific axis

By default, both horizontal and vertical gridlines are added by the matplotlib.pyplot.grid() function. You can, however, specify the axis to which you’d like to add the gridlines.

For example, to add gridlines only on the x-axis (vertical gridlines), pass axis='x' as an argument.

# plot x and y on scatter plot
plt.scatter(x, y)

# add axes labels
plt.xlabel('Year')
plt.ylabel('1USD in INR')

# add gridlines
plt.grid(visible=True, axis='x')

Output:

scatter plot with vertical gridlines

We get gridlines only on the x-axis.

Similarly, you can get gridlines only on the y-axis (horizontal gridlines) by passing axis='y'.

# plot x and y on scatter plot
plt.scatter(x, y)

# add axes labels
plt.xlabel('Year')
plt.ylabel('1USD in INR')

# add gridlines
plt.grid(visible=True, axis='y')

Output:

scatter plot with horizontal gridlines

We get only the horizontal gridlines.

Example 3 – Change gridline properties

The matplotlib.pyplot.grid() function also allows you to pass keyword arguments to customize the line properties of the gridlines. For example, let’s change the color of the gridlines to green.

# plot x and y on scatter plot
plt.scatter(x, y)

# add axes labels
plt.xlabel('Year')
plt.ylabel('1USD in INR')

# add gridlines
plt.grid(visible=True, color='g')

Output:

scatter plot with green gridlines

The gridlines in the above plot are green.

You can similarly change other line properties such as the linestyle, the linewidth, etc. For a more comprehensive list of line properties, refer to the documentation.

FAQs

How to add only horizontal gridlines to a plot in matplotlib?

To add only horizontal gridlines (y-axis gridlines), pass axis='y' as an argument to the matplotlib.pyplot.grid() function.

How to add only vertical gridlines to a plot in matplotlib?

To add only vertical gridlines (x-axis gridlines), pass axis='x' as an argument to the matplotlib.pyplot.grid() function.

How to disable (or hide) gridlines in a matplotlib plot?

To hide (or not show) gridlines, pass visible=False as an argument to the matplotlib.pyplot.grid() function.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush

    Piyush is a data scientist passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.