Signup/Sign In
LAST UPDATED: OCTOBER 20, 2020

Matplotlib Grids

In this tutorial, we will cover what are grids in a figure, and how it be customized using the grid() function in the Matplotlib.

What are Grids?

In any chart or graphical representation of any set of data, grids are made so that you can better understand the whole graph/chart and relate the points on the plot with the scale values, as there are grid lines in the background. Grid makes the inner part of a graph/chart basically made up of intersecting lines either straight (vertical, horizontal, and angular) or curved lines that are mainly used to represent the data.

  • With the help of grids in matplotlib you can gain a better understanding of graphs.

  • You can easily get a reference for the data points.

  • matplotlib.pyplot.grid() is a function that is used to create grids easily and you can also customize as there are many options available.

Matplotlib grid() Function

This function is basically used to create the grid.

  • In axes object the grid() function is used to set the visibility of the grid inside the figure. It can be either on or off.

  • The linestyle and linewidth properties can be set in the grid() function.

  • You can customize the grid according as per your requirements as there are many available options.

Matplotlib grid() Syntax

Below we have the basic syntax to use the function matplotlib.pyplot.grid() function:

matplotlib.pyplot.grid(b, which, axis, **kwargs)

Let us discuss about the parameters used in this function:

  • b

    This parameter indicates a bool value which is used to specify whether to show grid-lines or not. The default value of this parameter is True.

  • which

    This parameter is used to indicate the grid lines on which there is a need to apply the change. There are three for values for this: major, minor, or both.

  • axis

    This parameter is used to denote the axis on which there is a need to apply changes. The values for this are x, y, or both.

  • **kwargs

    This parameter is used to indicate the optional line properties.

Example1

Let us take a look at an example where we will create a grid in the graph:

import numpy as np
import matplotlib.pyplot as plt

t = np.arange(0.0, 1.0 + 0.01, 0.01)
s = np.cos(2 * 2*np.pi * t)
t[41:60] = np.nan

plt.subplot(2, 1, 1)
plt.plot(t, s, '-', lw=2)

plt.xlabel('time (s)')
plt.ylabel('voltage (mV)')
plt.title('A sine wave having gap of NaNs between 0.4 and 0.6')
plt.grid(True)

plt.subplot(2, 1, 2)
t[0] = np.nan
t[-1] = np.nan
plt.plot(t, s, '-', lw=2)
plt.title('Graph with NaN in first and last point')

plt.xlabel('time (s)')
plt.ylabel('more nans')
plt.grid(True)

plt.tight_layout()
plt.show()

The output for the above code snippet is as follows:
using matplotlib grid function to change grid

In the function above, all we have done is added the plt.grid(True), which shows the grid in the final graph.

Example 2

Now in the example given below, we will show you how to use various options to customize the graph:

import matplotlib.pyplot as plt 
import numpy as np 
  
x = np.linspace(0, 2 * np.pi, 400) 
y = np.sin(x ** 2) 

plt.plot(x, y, 'orange') 

plt.title("Customized Plots") 

# customize grids
plt.grid(True, color = "black", linewidth = "1.4", linestyle = "-.") 
  
plt.show() 

The output for the above code will be as follows:

custom grid in matplotlib example

In the above figure, you can see the grid lines are made of -. which we have specified using the linestyle parameter, the width of line is specified as 1.4 which controls the width of the line. We have also specified the plot color to be orange, which we can see in the output.

Summary:

The grid() function can be used to make better grids in your graph. You can try different styles, color and width for grid lines to make your graph visually better.



About the author:
Aspiring Software developer working as a content writer. I like computer related subjects like Computer Networks, Operating system, CAO, Database, and I am also learning Python.