How to set the line properties of a grid in matplotlib

Overview

Plots in matplotlib represent the visualization of a given data. These plots require numerous properties to enhance the visuals. One of these properties is the gridline.

A gridline is a series of intersecting straight lines that show axis division. Apart from data visualization tools like Microsoft Excel, pandas, powerbi, and so on, the matplot library (matplotlib) also comes with several features. These features enhance and change the visualization (figure, plot, etc.) of a data set. In this shot, we’ll learn about the feature of matplotlib that helps us set line properties of a gridline in a plot.

Before we proceed, let’s see how we can add a grid to a plot in matplotlib

Adding gridlines to a plot

The pyplot module in matplotlib has a grid() function that adds a grid line to a plot.

Example

The following example demonstrates the use of pyplot.grid() to add a grid to a plot:

import matplotlib.pyplot as plt
import numpy as np
x= np.array([1, 5])
y = np.array([10, 20])
plt.plot(x, y)
# usig the title() method
plt.title('Y against X')
# using the xlabel() and ylabel() methods
plt.xlabel("x axis")
plt.ylabel("y axis")
# adding the grid using the grid() function
plt.grid()
plt.show()

Code Explanation

  • Line 1: We import the pyplot module from the matplot library
  • Line 2: We import the numpy module.
  • Line 4: We create an array variable x using the numpy.array() method.
  • Line 5: We create another array variable y using the numpy.array() method.
  • Line 7: We plot the y against x ( x on the x-axis and y on the y-axis) using the pyplot.plot() method.
  • Line 10: We give a title to our plot using the pyplot.title() method.
  • Line 12: We give a label to the x-axis of our plot using the pyplot.xlabel() method.
  • Line 13: We give a label to the y-axis of our plot using the pyplot.ylabel() method.
  • Line 15: We add a grid to the plot using the pyplot.grid() function.
  • Line 16: It shows us our plot using the pyplot.show() method.

Properties of a grid line

The pyplot.grid() function takes the following parameters:

  • color: Specifies the color we want. For example, red, green, yellow, black, and so on.
  • linestyle- Specifies the style we want on the line. For example, dashed ’—‘, solid ’-‘, dashdot ’-.’, and so on.
  • linewidth: specifies the grid line’s size. Positive integers are the values.

This function is always written like the code below:

grid(color = 'color', linestyle = 'linestyle', linewidth = number).

Code

import matplotlib.pyplot as plt
import numpy as np
x= np.array([1, 5])
y = np.array([10, 20])
plt.plot(x, y)
# usig the title() method
plt.title('Y against X')
# using the xlabel() and ylabel() methods
plt.xlabel("x axis")
plt.ylabel("y axis")
# adding the grid using the grid() function properties
plt.grid(color = 'red', linestyle = '-', linewidth=5)
plt.show()

Explanation

The code above is similar to the first one with some minor changes:

  • Line 15: We set properties for the pyplot.grid() where the color we chose is red, linestyle = solid, and the linewidth of 5 for the grid.

Free Resources