Get Started

Creating small multiples with plt.subplots

Small multiples are used to plot several datasets side-by-side. In Matplotlib, small multiples can be created using the plt.subplots() function. The first argument is the number of rows in the array of Axes objects generate and the second argument is the number of columns. In this exercise, you will use the Austin and Seattle data to practice creating and populating an array of subplots.

The data is given to you in DataFrames: seattle_weather and austin_weather. These each have a "MONTH" column and "MLY-PRCP-NORMAL" (for average precipitation), as well as "MLY-TAVG-NORMAL" (for average temperature) columns. In this exercise, you will plot in a separate subplot the monthly average precipitation and average temperatures in each city.

This is a part of the course

“Introduction to Data Visualization with Matplotlib”

View Course

Exercise instructions

  • Create a Figure and an array of subplots with 2 rows and 2 columns.
  • Addressing the top left Axes as index 0, 0, plot the Seattle precipitation.
  • In the top right (index 0,1), plot Seattle temperatures.
  • In the bottom left (1, 0) and bottom right (1, 1) plot Austin precipitations and temperatures.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Create a Figure and an array of subplots with 2 rows and 2 columns
fig, ax = plt.subplots(____, ____)

# Addressing the top left Axes as index 0, 0, plot month and Seattle precipitation
ax[0, 0].plot(____, ____)

# In the top right (index 0,1), plot month and Seattle temperatures
ax[0, 1].plot(____, ____)

# In the bottom left (1, 0) plot month and Austin precipitations
ax[____].plot(____, ____)

# In the bottom right (1, 1) plot month and Austin temperatures
ax[____].plot(____, ____)
plt.show()

This exercise is part of the course

Introduction to Data Visualization with Matplotlib

BeginnerSkill Level
4.5+
107 reviews

Learn how to create, customize, and share data visualizations using Matplotlib.

This chapter introduces the Matplotlib visualization library and demonstrates how to use it with data.

Exercise 1: Introduction to data visualization with MatplotlibExercise 2: Using the matplotlib.pyplot interfaceExercise 3: Adding data to an Axes objectExercise 4: Customizing your plotsExercise 5: Customizing data appearanceExercise 6: Customizing axis labels and adding titlesExercise 7: Small multiplesExercise 8: Creating a grid of subplotsExercise 9: Creating small multiples with plt.subplots
Exercise 10: Small multiples with shared y axis

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free