Categories
DATA MINING DATA SCIENCE DATA VISUALIZATION PROGRAMMING PYTHON TUTORIALS

How To Add Grid To A Matplotlib Plot Graph Using Python

In this tutorial, we will learn how to add grid to a Matplotlib plot graph using Python. If we look at our previous article, we had learnt how to draw multiple lines on a graph using Matplotlib.

However, if we take a look at the graph it had produced, we see that it’s background was mostly blank. But wouldn’t it be nice if we could get some sort of reference grid lines in there? This will help is interpreting the chart way better; is’nt it? Then let us see how we can achieve this.

Finding The Function To Add Grid To A Matplotlib Plot Graph

The first thing that we need to do now is to see if Matplotlib library already provides a function to add grid to our plot. By checking through its documentation, we do come across it’s grid function. But what does it do?

Grid function in Python Matplotlib – What does it do?

By looking into the Matplotlib’s documentation, we can see that the grid function has the following signature:

grid(b=None, which='major', axis='both', **kwargs)

From the above signature, we see that the grid function takes in 3 parameters. However, all these four parameters are optional. So we can simply call plt.grid() function and it should still work!

Understanding the grid function parameters

If we take a look at each of these parameters in the documentation, we see that they do simple operations:

  • b – This is a boolean value which simply turns the grid lines ON or OFF
  • which – This parameter indicates which grid lines to make changes on.
  • axis – This parameter indicates which axis to make changes on.
  • kwargs – This parameter will define the properties of the grid lines.

Hence, we can see here that we simply need to use this Matplotlib grid function to get our background grid lines working in our plot.

Based on this, we can then rewrite our program from our previous article as follows:

import matplotlib.pyplot as plt
x = range(1, 10)
plt.plot(x, [xi*1 for xi in x])
plt.plot(x, [xi*2 for xi in x])
plt.plot(x, [xi*3 for xi in x])

So we can see that the code is pretty much the same for the first 5 lines. However before we call the show() to display our plot, we will add the grid lines. We do this by adding our grid() as follows:

plt.grid()
plt.show()

If you look at the above code, you will see that we are simply calling the grid() without any parameters. So we are letting the function just use its default values.

Final Code To Add Grid To A Matplotlib Plot Graph

So here is the full code that you can use in your Python interpreter to get the plot with grid lines in the background of our figure:

import matplotlib.pyplot as plt
x = range(1, 10)
plt.plot(x, [xi*1 for xi in x])
plt.plot(x, [xi*2 for xi in x])
plt.plot(x, [xi*3 for xi in x])

plt.grid()
plt.show()

With this code, we finally get the following image:

Final output of the code we modified to add grid to a Matplotlib plot graph
Final output of the code we modified to add grid to a Matplotlib plot graph

With this, we complete our tutorial here. Hope this explaination on how we can add grid to a Matplotlib plot graph using Python was clear. If you still have any doubts, do let me know in the comments below. So until next time, ciao!

1 reply on “How To Add Grid To A Matplotlib Plot Graph Using Python”

Leave a Reply

Your email address will not be published. Required fields are marked *