Sometimes, we want to display two lines side-by-side, rather than in the same set of x- and y-axes. When we have multiple axes in the same picture, we call each set of axes a subplot. The picture or object that contains all of the subplots is called a figure.
We can have many different subplots in the same figure, and we can lay them out in many different ways. We can think of our layouts as having rows and columns of subplots. For instance, the following figure has six subplots split into 2 rows and 3 columns:
We can create subplots using .subplot()
.
The command plt.subplot()
needs three arguments to be passed into it:
- The number of rows of subplots
- The number of columns of subplots
- The index of the subplot we want to create
For instance, the command plt.subplot(2, 3, 4)
would create “Subplot 4” from the figure above.
Any plt.plot()
that comes after plt.subplot()
will create a line plot in the specified subplot. For instance:
# Data sets x = [1, 2, 3, 4] y = [1, 2, 3, 4] # First Subplot plt.subplot(1, 2, 1) plt.plot(x, y, color='green') plt.title('First Subplot') # Second Subplot plt.subplot(1, 2, 2) plt.plot(x, y, color='steelblue') plt.title('Second Subplot') # Display both subplots plt.show()
This would result in a figure with the two plots arranged like this:
Instructions
We have defined the lists months
, temperature
, and flights_to_hawaii
for you. Using the plt.subplot
command, plot temperature
vs months
in the left box of a figure that has 1 row with 2 columns.
Plot flights_to_hawaii
vs temperature
in the same figure, to the right of your first plot. Add the parameter "o"
to the end of your call to plt.plot
to make the plot into a scatterplot, if you want!
Show the plots.