Matplotlib · Chapter 13 of 16
Mosaics and layouts that fit
By the end of this chapter you can lay out panels of different sizes with plt.subplot_mosaic (and the same by numbers with GridSpec), place an inset exactly with fig.add_axes, make a figure fit itself with layout="constrained", say a title, a label or a legend once for the whole figure with fig.suptitle, fig.supxlabel, fig.supylabel and fig.legend, and know what tight_layout and subplots_adjust do. 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
Ira's monthly page for each housing society answers three questions about the society's air: how was the year, what is a normal day like, and how does the city compare. The year is a long series and needs the whole width; the other two are small. Chapter 12's grids make every panel the same size, so the year gets a third of the page and the day gets as much room as the year.
And every grid in the last chapter had the same flaw: a panel's title ran into the tick labels of the panel above it. With labels on every panel, four charts in the default figure put Delhi's x axis label on top of Noida's title:
<!-- chart: !Four cities in March 2026 in a 2 by 2 grid in the default layout: the top panels' x axis labels sit on the bottom panels' titles, Noida and Faridabad half hidden -->
A reader sees words written over words. This chapter gives each panel the size its question needs, and makes Matplotlib find the room for every label by itself.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
station_report.py: a station's report page
Write station_report(days, pm25, months, monthly) in station_report.py. days is a pandas Series of dates and pm25 a list of one station's daily PM2.5 on them, from daily.csv with the blank days left out; months is a list of month names (Oct, Nov, ...) and monthly a list of the station's mean PM2.5 in each month, in the same order. It makes one Figure with exactly three Axes laid out as a mosaic (plt.subplot_mosaic, or a GridSpec):
- on top, one panel across the whole width: pm25 against days as one line;
- bottom left: a histogram of pm25 (any bins);
- bottom right, beside it: one bar per month, the bars' heights from monthly and their labels from months, in that order.
It returns the Figure. The Check tries Saket (SH-DEL-07) over the year and SH-DEL-20, which started in January 2026 (nine months).
- Task 2
fitted_grid.py: fit the grid and say it once
Write city_grid(days, by_city, title) in fitted_grid.py. days is a list of day numbers and by_city a dict from city_daily.csv that maps four cities' names to their daily PM2.5 on those days. It makes one Figure with a 2 by 2 grid of Axes, fitted with constrained layout (layout="constrained"), and draws each city as one line in a panel of its own, in the dict's order (the top row first), titled with the city's name exactly. The whole figure gets the title it is given (fig.suptitle, exactly title) and one y label for all the panels, exactly PM2.5 (µg/m³) (the starter keeps it in Y_LABEL); no panel has a y label of its own. It returns the Figure, with four Axes.
The Check tries Delhi, Gurugram, Noida and Faridabad in January 2026 titled January 2026, day by day, and four cities in May 2026 titled May 2026.
- Task 3
one_legend.py: one legend for four panels
Write site_panels(hours, diwali, normal) in one_legend.py. hours is the list 0 to 23; diwali and normal are dicts from diwali_hourly.csv with the same four sites as keys, each site's 24 readings on 20 October 2025 (diwali) and on 16 October 2025 (normal). It makes one Figure with a 2 by 2 grid of Axes fitted with constrained layout. Each panel, in the dicts' order (the top row first), has two lines, the site's diwali readings and its normal readings against hours, and is titled with the site exactly. The Figure has one legend of its own (fig.legend) with exactly two entries in this order, Diwali then Normal day, placed below all the panels, outside them (loc="outside lower center"); no panel has a legend. It returns the Figure.
The Check tries two sets of four stations, and reads where the legend was drawn.
- Task 4 · interview question
Interview: fit_fix.py, a page that fits
Ira's report_page(days, by_city, title) draws four cities in a 2 by 2 grid, each panel with one line, titled with its city and its x axis labelled Day of the month, under a figure title (fig.suptitle). She called fig.tight_layout() to make it fit, yet the bottom panels' titles run into the top panels' axis labels and the figure title sits on the first panels' titles. days is a list of day numbers and by_city a dict from city_daily.csv of four cities' daily PM2.5. Fix it so the figure is made with constrained layout (layout="constrained", and no tight_layout), keeping its four panels, their lines and titles, and the figure title exactly as given. The Check measures where every panel title, x axis label, tick label under a panel and the figure title were drawn: none may overlap another's. It returns the Figure.
The Check tries four cities in March 2026 titled March 2026, and four in August 2026 titled August 2026.
Tanvir will ask: why constrained layout, and what can tight_layout not do?
What you will learn
- plt.subplot_mosaic(layout): panels of different sizes from a picture of the page, returned as a dict of Axes by name.
- width_ratios and height_ratios: how wide each column and how tall each row.
- fig.add_gridspec and gs[0, :]: the same layout by numbers.
- fig.add_axes([left, bottom, width, height]): one Axes exactly where you say, such as an inset.
- layout="constrained": the Figure makes room for every title and label.
- fig.suptitle, fig.supxlabel, fig.supylabel, fig.legend: say it once for all the panels.
- tight_layout and subplots_adjust: the older, one-time ways to fit, and where they fall short.
More in Many charts
Build "Mosaics and layouts that fit" 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.