Matplotlib · Chapter 15 of 16

Styles and reusable charts

By the end of this chapter you can change how every chart looks with a style sheet (plt.style.use, or better with plt.style.context(...)), read and set Matplotlib's settings (rcParams, plt.rc), write SaafHawa's own .mplstyle file, and write chart functions that draw on the Axes they are given, so one function serves a single chart, a grid and a mosaic, and a loop of reports leaves no figure open. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Real work
  • 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

SaafHawa now sends charts every day: the schools' morning notes, the societies' monthly report, Lalitha's investor deck. Ira has noticed that no two look alike. One script hides the top and right spines, another forgets; one is 8 inches wide, the next 6.4; Gurugram is orange in the report and green in the deck. Each script repeats the same six lines of settings, and each copy drifts.

The charts themselves have the same problem. Jaspreet's daily line, the city bars and the hour profile are each written inside a function that makes its own Figure, so none of them can go into the monthly mosaic, and Ira copies their code into every panel. Tanvir's answer is two habits: set the look once, in a style, and write each chart as a function that draws on an Axes it is given.

What you build: 4 tasks

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

  1. Task 1

    house_chart.py: SaafHawa's house style

    Two files. First, finish saafhawa.mplstyle: keep its four lines (figure.figsize: 8, 4.5, font.size: 11, the axes.prop_cycle line with SaafHawa's green 0b6e4f first, grid.alpha: 0.3) and add these four, exactly:

    axes.spines.top: False

    axes.spines.right: False

    axes.grid: True

    axes.grid.axis: y

    Matplotlib must be able to read every line of the file.

    Then write house_chart(hours, pm25, style) in house_chart.py. style is the path of a style file. Use that style for this chart only (inside a with block), and make the Figure and its one Axes with plt.subplots() while it is in use; draw pm25 against hours as one line, title the chart PM2.5, hour by hour (exactly), label the y axis PM2.5 (µg/m³) (Y_LABEL in the starter), and return the Figure.

    With saafhawa.mplstyle, the Figure is 8 by 4.5 inches, its one line is SaafHawa's green, the top and right spines are hidden, and there is a grid on the y axis only. The Check also gives it another style file at another path (a 5 by 5 inch figure, a red line), and checks that Matplotlib's settings are as they were once the function returns.

  2. Task 2

    pm_line.py: a line you can put on any chart

    Write pm_line(ax, x, y, , label=None, *kwargs) in pm_line.py. It draws y against x as one line on ax, the Axes it is given, with label as the line's label, and passes every other keyword argument (kwargs) on to that same drawing call. It labels ax's y axis PM2.5 (µg/m³) (Y_LABEL in the starter) and returns ax, the same Axes.

    It makes no Figure and uses nothing from plt. The Check makes the Figure and calls pm_line on its one Axes with label="Delhi" (the line is labelled Delhi), and with color="C3" and linestyle="--" and no label (the line is drawn in C3, dashed, and has no label of its own). Then it makes a 1 by 2 and calls pm_line on the left Axes and then on the right one: each Axes must hold its own one line.

  3. Task 3

    station_report.py: a chart for every station, none left open

    Write two functions in station_report.py.

    save(fig, path, dpi=150) saves fig as a PNG at path at that dpi, then closes fig. The Check saves a new figure with save(fig, path) (the file must be 960 by 720 pixels, at 150 dpi) and with dpi=50 (320 by 240 pixels), and the figure must no longer be open afterwards.

    station_report(days, folder) is given rows of daily.csv with three columns, date (as dates), station_id and pm25, for several stations. For each station, in the order the stations first appear in days, it makes a Figure with one Axes, draws that station's pm25 against its dates as one line, titles the chart with its station_id (SH-NOI-01), and saves it with save as <station_id>.png in folder. It returns the list of the paths it saved (text or Path), one per station, in that order.

    The Check tries Noida's seven stations in September 2026 and two Faridabad stations (SH-FBD-02 and SH-FBD-04) in February 2026, each in a folder of its own (one with a space in its name). It reads what each file's figure held when it was saved, and no figure may be left open when station_report returns.

  4. Task 4 · interview question

    Interview: two_panels.py, a panel that will not compose

    Jaspreet's pm_panel(x, y, title) draws one line with a title, in a Figure it makes itself. Ira's side_by_side(x, left, right, left_title, right_title) puts two of them in a 1 by 2: PM2.5 on the left, PM10 on the right. The Figure side_by_side returns has two empty panels, and two more figures are left open.

    Fix both functions. pm_panel becomes pm_panel(ax, x, y, title): it draws y against x as one line on ax with title as the chart's title, returns ax, and makes no Figure. side_by_side returns its Figure with two Axes: left against x on the left titled left_title, right against x on the right titled right_title.

    The Check calls pm_panel on an Axes of its own (no new Figure may be made), then side_by_side(dates, pm25, pm10, "PM2.5", "PM10") for Delhi's November 2025 and Noida's July 2026. Only side_by_side's Figure may be left open.

    Tanvir will ask: why should a chart function take an Axes and never make a figure of its own?

What you will learn

  • plt.style.available and plt.style.use: Matplotlib's ready-made styles, for the whole program.
  • with plt.style.context(...): a style for one block only (preferred).
  • matplotlib.rcParams and plt.rc: the settings a style changes.
  • A .mplstyle file: your own style, used by its path.
  • What wins: an argument in the call, then the style in use, then rcParams, then the defaults.
  • def chart(ax, ..., kwargs)**: a chart function that draws on the Axes it is given and returns it.
  • Size for where it goes, a save helper, and a loop that closes every figure.

More in Real work

Build "Styles and reusable charts" 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.