Several charts in one figure
Last updated
Four stores, four lines: on one chart they tangle, and on four separate charts they're hard to compare. A figure with a grid of charts, all on the same scale, does both jobs. In this lesson you'll make grids with subplots, lay out uneven figures with subplot_mosaic, let constrained layout keep the labels apart, and see how small multiples on different scales mislead.
By the end of this lesson you can
- Make a grid of charts with plt.subplots(nrows, ncols) and loop over it
- Lay out an uneven figure with plt.subplot_mosaic
- Share axes so small multiples can be compared honestly
A grid of charts
plt.subplots takes a number of rows and columns. The first cell makes a table of each store's daily rice, one column per store (pivot, 4.4). Run all three cells.
plt.subplots(2, 2)gives back the figure andaxs, a NumPy array of axes with shape(2, 2)(2.4), as the second cell prints. Its empty figure shows the four charts waiting to be drawn on.axs[0, 1]is the top right.axs.flatwalks through them one by one, row by row, sozip(axs.flat, stores)pairs each chart with a store, and the loop draws the same kind of chart four times. That's the whole trick: write the chart once, in a loop.layout="constrained"moves the charts apart until no title or tick label overlaps another. Without it, titles of the bottom row can run into the dates of the top row. Use it on every figure with more than one chart.
Uneven layouts with subplot_mosaic
Not every figure is a neat grid. Often one chart is the main one and the others support it. plt.subplot_mosaic lets you draw the layout with names: each inner list is a row, and a name repeated across cells makes one chart that spans them. Try some layouts here; a letter is a chart, and a dot is a gap.
fig, axd = plt.subplot_mosaic([
["A","A","B"],
["A","A","C"],
], layout="constrained")
axd["A"].set_title("A")
axd["B"].set_title("B")
axd["C"].set_title("C")Each name must make a rectangle. An L-shape can't be one chart, and matplotlib stops with a ValueError. Now a real one: the average store, big, with Batangas and Rosario beside it. Run the cells.
axdis a dictionary of axes, keyed by the names you used (the last cell lists them), soaxd["all"]is the big chart. Names are easier to read thanaxs[0, 2].sharey=Truegives all three charts one y-scale, so Batangas and Rosario can be compared with each other and with the average.tick_params(labelbottom=False)hides the small charts' dates; the big chart's dates serve for all three.
Before subplot_mosaic, uneven layouts were made with GridSpec, and you'll meet it in older code: gs = fig.add_gridspec(2, 3), then fig.add_subplot(gs[:, :2]) for a chart over two columns. It does the same job with slices instead of names.
Wrong way: small multiples on different scales
Small multiples are a row or grid of the same chart, one per group, so the eye can compare them. The idea is old, and Edward Tufte named it. It only works if every panel means the same thing. Run the first two cells.
The second cell's four panels look alike: four wiggly lines filling their boxes. Rosario looks as busy as Batangas. But look at the tick labels: each panel chose its own y-range to fit its own data, so Rosario's axis runs from about 15 to 35 and Batangas's from about 30 to 70. Every panel is stretched to fill its box, and the sizes disappear.
The third cell adds sharey=True and starts the shared axis at zero. Now Batangas sits clearly above the rest and Rosario well below, about half of Batangas. Same data, honest comparison.
The check: small multiples share their axes unless there's a reason not to. If they really need different scales (to show each store's own ups and downs), say so in the title, so no one compares the heights.
Check what you learned
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- matplotlib: Arranging multiple Axes in a Figure: subplots, GridSpec and subplot_mosaic
- matplotlib: Complex and semantic figure composition (subplot_mosaic): layouts drawn as lists of names
- matplotlib: Constrained layout guide: keeping titles and labels from overlapping
- matplotlib: matplotlib.pyplot.subplots: nrows, ncols, sharex and sharey