Subplots and constrained layout

3.11.2

Arrange several plotting areas and share scales when the measurements are comparable.

On this page

Organize plots in a grid

subplots creates a Figure plus Axes. For a 1×2 grid, unpack the two Axes or use squeeze=False to always receive a 2D array. Use layout="constrained" to help fit axes labels and titles.

import matplotlib.pyplot as plt

fig, (left, right) = plt.subplots(1, 2, layout="constrained")
left.plot([2, 4, 3])
left.set_title("Daily volume")
right.bar(["Tea", "Cake"], [6, 4])
right.set_title("Item volume")
plt.show()
A line chart of daily volume and a bar chart of item volume sit side by side. Download image

Share axes when useful

sharex and sharey link axes. Share a scale when units and ranges should be directly comparable. For charts with different units, label each Axes clearly.

Official references