Skip to lesson content
navan✳THE BUILD LABPYTHON 3
0/101 complete
BUILD LAB / PYTHON/ Make the data visible
EXPERIMENT 55 / 101

One canvas, several questions.

Compose multiple views without making their scales ambiguous.

ADVANCED LAB
BY NAVAN SAMI◷ 11 MIN READPYTHON 3

01 The idea

A multi-panel figure lets the reader compare related views while keeping each panel focused. plt.subplots returns a Figure and one or more Axes objects. When panels measure the same quantity, shared scales can make comparison more honest. Layout management and descriptive panel titles help the figure work outside your notebook.

02 The experiment

Read it once. Predict what will happen. Then copy it into a .py file and run it in your local Python setup.

matplotlib_subplots.py
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
fig, axes = plt.subplots(1, 2, figsize=(9, 3.5), layout="constrained")
axes[0].bar(["Build", "Test"], [40, 20], color="#ed5027")
axes[0].set(title="Effort", ylabel="Minutes")
axes[1].plot([1, 2, 3], [2, 5, 7], "o-", color="#48643a")
axes[1].set(title="Completion", xlabel="Day", ylabel="Tasks")
fig.suptitle("Orbit: two useful views")
fig.savefig("matplotlib-subplots.png", dpi=140, bbox_inches="tight")
plt.close(fig)
print("Saved matplotlib-subplots.png")
↳ EXPECTED OUTPUT
Saved matplotlib-subplots.png
Subplots & shared axes: chart generated by the Python example
THE OUTPUT, MADE VISIBLE · Generated from this lesson’s example.
Download chart ↗

03 Under the hood

  1. 1

    With one row and two columns, axes is a one-dimensional array of Axes. Other subplot shapes can produce a two-dimensional array; squeeze=False makes that shape consistent when needed.

  2. 2

    Each panel gets its own labels and units. These two panels measure different quantities, so sharing their y-axis would be misleading.

  3. 3

    layout="constrained" helps reserve space for labels and titles. Still inspect the exported image, especially when labels are long or a legend sits outside a panel.

04 Your turn, builder

A SMALL MISSION

Create two side-by-side bar charts for different teams using a shared y-axis.

Reveal one possible solution +
one_possible_solution.py
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
fig, axes = plt.subplots(1, 2, figsize=(8, 3.5), sharey=True, layout="constrained")
for ax, team, values in zip(axes, ["Orbit", "Atlas"], [[5, 7], [3, 6]]):
    ax.bar(["Week 1", "Week 2"], values, color="#ed5027")
    ax.set(title=team, ylim=(0, 8))
axes[0].set_ylabel("Completed tasks")
fig.savefig("challenge.png", dpi=140, bbox_inches="tight")
plt.close(fig)
print("Saved challenge.png")

05 Check your signal

QUICK SIGNAL CHECK

When is a shared y-axis useful?

THAT’S ANOTHER IDEA IN YOUR TOOLKIT.

Experiment. Logged.

When you can explain the idea and make the example your own, mark it complete. Your log stays in this browser.

Keep exploring

Go deeper with the official Python guide ↗.

Built with curiosity. By Navan Sami. ↗Python 3 · Think. Try. Build.