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:
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:
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).
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
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:
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:
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:
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
To add only horizontal gridlines (y-axis gridlines), pass axis='y'
as an argument to the matplotlib.pyplot.grid()
function.
To add only vertical gridlines (x-axis gridlines), pass axis='x'
as an argument to the matplotlib.pyplot.grid()
function.
To hide (or not show) gridlines, pass visible=False
as an argument to the matplotlib.pyplot.grid()
function.
You might also be interested in –
- Create a Scatter Plot in Python with Matplotlib
- Change Background Color of Plot in Matplotlib
- Add Trendline to a Maplotlib Plot with Code and Output
- How to Add Title to a Plot in Matplotlib? (Code Examples with Output)
- Set Axis Range (axis limits) in Matplotlib Plots
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.