Session Ready
Exercise

Subfigures in matplotlib

Sometimes you want to combine several subplots in a single figure.

You can do this by creating a figure and an axes simultaneously by using the plot.subplots() function. This function returns the figure and the axes objects:

import matplotlib.pyplot as plt

# Create a figure and axes 
fig, (ax1, ax2) = plt.subplots(number_of_rows, number_of_columns)

You can now use these axes objects to generate plots:

# Use the axes to plot visualizations
ax1.hist(df['column_1'])
ax2.hist(df['column_2'])
plt.show()
Instructions 1/2
undefined XP
  • 1
  • 2
  • Create a figure with one axes.
  • Use the ax axes object to draw a scatter plot of the 'tip' and 'total_bill' columns.