Matplotlib · Chapter 12 of 16

Grids of charts with subplots

By the end of this chapter you can make a Figure with a grid of Axes with plt.subplots(nrows, ncols), pick a panel out of the array it gives back without falling into the shape trap, draw a set of small multiples with axes.flat and zip, share the axes so the panels can be compared, remove the panels left over, and size the figure for its grid. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Many charts
  • 4 tasks
  • Matplotlib
  • In the all labs plan

The first 2 chapters of Matplotlib are free. This one comes with the all labs plan.

Why you need this

Gurugram's residents' welfare associations share one newsletter, and Lalitha wants a page in it about SaafHawa's eight roofs in the city: has every neighbourhood had the same year? Drawn on one Axes, the eight yearly lines are a tangle. You can tell winter from monsoon, and nothing else: which colour is Sohna Road?

<!-- chart: !Gurugram's eight stations day by day over the year on one Axes, eight colours knotted together, impossible to follow any one line -->

Eight separate figures are no better: eight files, each with its own scale, and the reader flips between them. What the page needs is one figure with eight small charts in a grid, all alike, so the eye compares them panel to panel. These are small multiples, and they are the next step from the one Axes that plt.subplots() has given you so far.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    small_multiples.py: one panel per city

    Write city_panels(days, by_city) in small_multiples.py. days is a pandas Series of dates, and by_city a dict from city_daily.csv that maps each city's name to a list of its daily PM2.5 on those days. It makes one Figure with a grid of 2 rows by 3 columns of Axes, and draws each city's readings against days as one line in a panel of its own, in the dict's order (the top row from left to right, then the bottom row), each panel titled with its city's name exactly. The panels share the y axis (sharing x as well is fine), and every panel no city uses is removed or turned off, so no empty panel is left. It returns the Figure, with exactly one Axes per city.

    The Check tries the five cities over the whole year, and four cities (Noida, Delhi, Ghaziabad, Gurugram) in October 2025, which leaves two panels over.

  2. Task 2

    pm_pair.py: PM2.5 and PM10 side by side

    Write pm_pair(days, pm25, pm10, city) in pm_pair.py. days is a pandas Series of dates; pm25 and pm10 are lists of the city's daily readings on those days, from city_daily.csv. It makes one Figure with 1 row of 2 Axes, side by side:

    - the left panel draws pm25 against days as one line, is titled PM2.5 in <city> (PM2.5 in Delhi), and its y axis is labelled µg/m³ (the starter keeps it in UNIT);

    - the right panel draws pm10 against days as one line, titled PM10 in <city>.

    It returns the Figure, with exactly two Axes. The Check tries Delhi over the year and Noida from December 2025 to February 2026.

  3. Task 3

    grid_fix.py: the grid that would not draw

    Jaspreet's station_grid(hours, by_site) makes a 2 by 3 grid for the six Diwali stations and goes through them, but it stops before drawing a single line, with AttributeError: 'numpy.ndarray' object has no attribute 'plot'. hours is the list 0 to 23, and by_site a dict from diwali_hourly.csv that maps each station's site to a list of its 24 readings on one day. Fix it so each station gets a panel of its own in the 2 by 3 grid, in the dict's order (the top row from left to right, then the bottom row), with one line (its readings against hours) and its site as the panel's title, exactly. It returns the Figure, with six Axes.

    The Check tries Diwali (20 October 2025) and 21 October, with the stations in another order.

  4. Task 4 · interview question

    Interview: shared_scale.py, three panels, one scale

    Ira's three_sites(hours, by_site) puts three stations side by side for a school's parents: one row of three Axes, in the dict's order, each with one line (its readings against hours) and titled with its site. Every panel's line climbs to the top of its own chart, so the parents read all three roofs as equally bad. hours is the list 0 to 23 and by_site a dict from diwali_hourly.csv that maps three stations' sites to their 24 readings on one day. Fix it so all three panels read on one y scale: the same y limits on every panel, with every reading inside them. It returns the Figure, with three Axes in one row.

    The Check tries Dwarka Sector 10, Anand Vihar and Rohini Sector 9 on 16 October 2025, and Mayur Vihar, Saket and Punjabi Bagh on Diwali.

    Tanvir will ask: why share the y axis between panels, and what goes wrong without it?

What you will learn

  • plt.subplots(nrows, ncols): one Figure with a grid of Axes, returned as a NumPy array.
  • The shape trap: a row of panels is a 1-D array, a grid is 2-D, and axes[1] means different things.
  • axes.flat with zip: go through the panels one at a time, beside your data.
  • sharex=True, sharey=True: every panel on the same scale.
  • ax.remove() and ax.axis("off"): get rid of the panels left over.
  • figsize= for a grid and squeeze=False.

More in Many charts

Build "Grids of charts with subplots" yourself

SaafHawa runs air sensors across Delhi NCR. Principals, societies and investors need its numbers at a glance: you draw them. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Matplotlib are free. This one comes with the all labs plan.