CommencerCommencer gratuitement

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()

Cet exercice fait partie du cours

Python for R Users

Afficher le cours

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de code.

import matplotlib.pyplot as plt

# Create a figure with 1 axes
fig, ax = plt.____(1, 1)

# Plot a scatter plot in the axes
____.scatter(tips____, tips____)
plt.show()
Modifier et exécuter le code