One canvas, several questions.
Compose multiple views without making their scales ambiguous.
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.
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")Saved matplotlib-subplots.png

03 Under the hood
- 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
Each panel gets its own labels and units. These two panels measure different quantities, so sharing their y-axis would be misleading.
- 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
Create two side-by-side bar charts for different teams using a shared y-axis.
Reveal one possible solution +
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
When is a shared y-axis useful?
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 ↗.