Matplotlib - Multiplots



In general data visualization and plotting, multiplots, or multiple plots, refer to the creation of more than one plot within a single figure or canvas. These are useful when you want to compare different datasets or visualize various aspects of the same data.

The below image represents the 4 different plots in a single figure −

Multiplots Input

Multiplots in Matplotlib

Matplotlib provides tools for creating multiplots, and the most common approach is through the use of subplots. The subplot() is a function available in the pyplot module, and it provides good control over the arrangement of individual plots within the figure. For more advanced layout customization, GridSpec or Figure.add_subplot() functions can be used.

Creating Simple Multiplots

Creating simple multiplots involves the basic use of the subplot() function in Matplotlib. Simple multiplots are useful when you want to showcase different datasets or aspects of the same data in a clear and organized manner.

Example

In this example, a 2x2 grid of subplots is created, and individual data are plotted on each subplot.

 import matplotlib.pyplot as plt import numpy as np # Sample Data x = np.linspace(0, 2 * np.pi, 400) y1 = np.sin(x ** 2) y2 = np.cos(x) y3 = np.tan(x) y4 = np.exp(-x) * np.sin(2 * np.pi * x) # Creating a 2x2 grid of subplots fig, axs = plt.subplots(2, 2, figsize=(7,4)) # Plot data on individual subplots axs[0, 0].plot(x, y1) axs[0, 1].plot(x, y2, color='orange') axs[1, 0].plot(x, y3, color='green') axs[1, 1].plot(x, y4, color='red') # Set titles to subplots axs[0, 0].set_title('Sin Plot') axs[0, 1].set_title('Cos Plot') axs[1, 0].set_title('Tan Plot') axs[1, 1].set_title('Exponential Plot') # Display the Multiplots plt.tight_layout() plt.show() 

Output

On executing the above code we will get the following output −

Multiplots Example 1

Stacked Multiplots

Stacked multiplots are nothing but arranging subplots in one direction either vertically(top-and-bottom) or horizontally(side-by-side). When stacking in one direction, the plt.subplots() function returns a 1D NumPy array.

Example

This example creates two vertically stacked subplots, each showcasing different plots.

 import matplotlib.pyplot as plt import numpy as np # Sample Data x = np.linspace(0, 2 * np.pi, 400) y1 = np.sin(x**2) y2 = np.cos(x**2) # Creating vertically stacked subplots fig, axs = plt.subplots(2, figsize=(7,4)) fig.suptitle('Vertically stacked subplots') # Plotting data on individual subplots axs[0].plot(x, y1) axs[1].plot(x, y2) # Adding titles to subplots axs[0].set_title('Sin Plot') axs[1].set_title('Cos Plot') # Adjust the layout plt.tight_layout() # Displaying the multiplot plt.show() 

Output

On executing the above code we will get the following output −

Multiplots Example 2

Multiplots with Sharing Axes

By default, each subplot Axes is scaled individually. For sharing axes between subplots you can use the sharex and sharey parameters of the plt.subplots() function.

Example

This example creates two Horizontally stacked subplots with a shared y-axis.

 import matplotlib.pyplot as plt import numpy as np # Sample Data x = np.linspace(0, 2 * np.pi, 400) y1 = np.sin(x ** 2) y2 = np.cos(x) # Creating subplots with shared y-axis fig, (ax1, ax2) = plt.subplots(1, 2, sharey=True, figsize=(7, 4)) fig.suptitle('Multiplots with Shared y-Axis') # Plotting data on individual subplots ax1.plot(x, y1) ax2.plot(x, y2) # Adding titles to subplots ax1.set_title('Sin Plot') ax2.set_title('Cos Plot') # Adjust the layout plt.tight_layout() # Display the multiplots plt.show() 

Output

On executing the above code we will get the following output −

Multiplots Example 4
Advertisements