# Matplotlib - Subplots function

Matplotlib tutorial 2020-11-20 19:05:44# Matplotlib - Subplots () Function

The Matplotlib 'spyplot API has a handy function called subplots () which acts as a utility and helper wrapper to create common sub-graphics layouts, including the bounding figure object, in one call.

Plt.subplots (nrows, ncols)

The two integer arguments of this function specify the number of rows and columns of the sub grid -trace. The function returns a figure object and a tuple containing axis objects equal to nrows * ncols. Each axes object is accessible by its index. Here we create a 2 row by 2 column subgraph and display 4 different graphs in each subgraph.

import matplotlib.pyplot as plt fig, a = plt.subplots (2, 2) import numpy as np x = np.arange (1,5) a [0 ] [0] .plot (x, x * x) a [0] [0] .set_title ( 'square ') a [0] [1] .plot (x, np.sqrt (x)) a [0] [1] .set_title ( 'square root ') a [1] [0] .plot (x, np.exp (x)) a [1] [0] .set_title ( 'exp ') a [1] [1] .plot (x, np.log10 (x)) a [1] [1] .set_title (' log ') plt.show ()

The line of code above generates the following output -