Introduction
In data visualization, it is often necessary to plot multiple graphs in a single figure. Matplotlib allows us to achieve this using subplots. In this lab, we will learn how to create subplots in Matplotlib.
VM Tips
After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.
Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.
If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.
Import Libraries
First, we need to import the required libraries. We will be using Matplotlib and NumPy. NumPy is used to generate some sample data.
import matplotlib.pyplot as plt
import numpy as np
Generate Some Sample Data
We will generate some sample data that we will use to plot our graphs.
## Create some fake data.
x1 = np.linspace(0.0, 5.0)
y1 = np.cos(2 * np.pi * x1) * np.exp(-x1)
x2 = np.linspace(0.0, 2.0)
y2 = np.cos(2 * np.pi * x2)
Create Subplots Using subplots()
We will create subplots using subplots() function. We will create two subplots, one above the other.
## Create subplots
fig, (ax1, ax2) = plt.subplots(2, 1)
Set Title and Axes Labels
We will set the title and axis labels for our subplots.
## Set title and axis labels
fig.suptitle('A tale of 2 subplots')
ax1.set_ylabel('Damped oscillation')
ax2.set_xlabel('time (s)')
ax2.set_ylabel('Undamped')
Plot Data on Subplots
We will now plot our data on the subplots.
## Plot data on subplots
ax1.plot(x1, y1, 'o-')
ax2.plot(x2, y2, '.-')
Display the Plots
Finally, we will display the plots using plt.show().
## Display the plots
plt.show()
Summary
In this lab, we learned how to create subplots in Matplotlib. We used the subplots() function to create subplots and set the title, axis labels, and plot data on the subplots. By using subplots, we can display multiple graphs in a single figure, which is useful for data visualization.