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 ]  .plot (x, x * x) a   .set_title ( 'square ') a   .plot (x, np.sqrt (x)) a   .set_title ( 'square root ') a   .plot (x, np.exp (x)) a   .set_title ( 'exp ') a   .plot (x, np.log10 (x)) a   .set_title (' log ') plt.show ()
The line of code above generates the following output -