Matplotlib · Chapter 16 of 16

Capstone: Ira's dashboard and a chart to publish

By the end of this chapter you can build a monthly dashboard from raw tables, from the questions to the saved file (a mosaic of chart functions, each panel titled with what it found, fitted and saved at a fixed size), and turn one finding into a chart a newspaper can print: the sentence first, one colour for the story, no furniture, the numbers on the chart from the data, and files sized for the page. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

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

Everything SaafHawa sends now comes from this lab. Ira wants one page a month that a principal, a society secretary or an investor reads in five seconds: was the month bad, where was it worst, when in the day is it safest to be outside. A newspaper has asked Lalitha for one chart of Diwali night, as files its page can print. Both are the whole lab at once: shaping the data (chapters 1 to 14), choosing the chart, the words on it, the layout (chapters 11 to 13), the style and reusable functions (chapter 15), and the files (chapter 3).

What goes wrong at this point is rarely a Matplotlib error. It is a dashboard whose numbers come from the wrong rows, a title typed in for one month and sent for the next, a PNG with the right pixels and the wrong size of text. Tanvir works through two checklists, one for each, and this chapter shows both on charts of their own: Lalitha's investor page, and Delhi's worst day of the year.

What you build: 5 tasks

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

  1. Task 1

    panels.py: three panels that say what they found

    Write three chart functions in panels.py. Each draws on ax, the Axes it is given, returns ax, and makes no Figure. Each title states the finding, worked out from the arguments, word for word:

    day_panel(ax, dates, pm25): dates and pm25 are lists, a month's days (as dates) and Delhi's PM2.5 on each. Draw pm25 against dates as one line and a horizontal line across the chart at 60. Title: Over 60 on <n> of <days> days, where n is how many values are over 60 and days how many there are (Over 60 on 26 of 31 days).

    city_panel(ax, means): means is a pandas Series of each city's mean PM2.5, indexed by city, in any order. Draw horizontal bars, one per city labelled with its name, sorted so the worst city's bar is at the top. Title: <city> was the worst, at <its mean with one decimal> (Ghaziabad was the worst, at 139.5).

    hour_panel(ax, hours, means): two lists, the hours of the day and the mean PM2.5 at each. Draw means against hours as one line. Title: Cleanest at <the hour of the lowest mean, two digits>:00 (Cleanest at 14:00).

    The Check calls each on an Axes of its own with November 2025's and July 2026's numbers, then city_panel with three made-up cities and hour_panel with the hours 6, 7 and 8.

  2. Task 2

    dashboard.py: Ira's monthly dashboard

    Write month_dashboard(city_days, hourly, month, path) in dashboard.py. city_days is city_daily.csv and hourly is delhi_hourly.csv, both as read_csv reads them (dates and times as text); month is text like 2025-11. Reshape, then draw:

    From the month's rows of city_days (the date starts with month): Delhi's dates (as dates, with pd.to_datetime) and pm25, and every city's mean pm25 (grouped by city). From the month's rows of hourly: the mean pm25 of each hour of the day (the hour is the time's characters 11 and 12 as a whole number), hours 0 to 23 in order.

    Lay out a mosaic with figsize=(12, 8), fitted with constrained layout: a panel day across the whole top row, city bottom left, hour bottom right. Draw them with task 1's day_panel, city_panel and hour_panel (from panels import them), give the figure its own title SaafHawa air report, <month> (SaafHawa air report, 2025-11), save the figure at path once everything is drawn (a PNG, 1200 by 800 pixels), and return the Figure.

    The Check tries November 2025 and July 2026, with paths in a folder of its own, and checks every panel's data and title as task 1 says.

  3. Task 3 · interview question

    Interview: worst_city.py, every month looks the same

    Lalitha's city_chart(city_days, month) ranks the five cities for one month in the societies' newsletter. city_days is city_daily.csv as read_csv reads it; month is text like 2026-07. July's chart says Ghaziabad was the worst at 113.7, a winter-sized number in the cleanest month, and November's and April's charts show exactly the same bars.

    Fix it so the chart is the month's: one Axes with horizontal bars, one per city, each the mean pm25 of that city's days in the month, sorted with the worst at the top; the title <city> was the worst in <month>, at <its mean with one decimal>; the x axis labelled Mean PM2.5 (µg/m³) (X_LABEL in the starter). Return the Figure. The Check tries July 2026, November 2025 and April 2026.

    Tanvir will ask: the chart drew perfectly and still lied. Where did it go wrong, and what would you check before Ira sends it?

  4. Task 4

    diwali_chart.py: a chart ready to publish

    Write diwali_chart(night, site) in diwali_chart.py. night is rows of diwali_hourly.csv with time as dates (columns time, station_id, site, pm25): six sites over one night. site names one of them. Return a Figure with one Axes holding:

    one line per site of pm25 against time; site's line in "tab:red" and every other site's line in "lightgrey";

    no legend, and the top and right spines hidden;

    a title on the left, exactly <site> peaked at <peak> µg/m³ at <hour>:00, the peak being site's highest pm25 with one decimal and hour the hour of that reading in two digits (on Diwali night, Dwarka Sector 10 peaked at 435.2 µg/m³ at 23:00; the starter keeps µg/m³ in UNIT);

    an annotation that points at the peak, at its time and its value, whose text has the value with one decimal;

    the y axis labelled PM2.5 (µg/m³) (Y_LABEL).

    The Check tries Rohini Sector 9 and Mayur Vihar from 18:00 on 20 October to 06:00 on 21 October 2025, and Saket from 18:00 on 22 October to 06:00 on 23 October (Saket has blank hours that night).

  5. Task 5 · interview question

    Interview: print_files.py, the PNG with tiny text

    The newspaper asked for the Diwali chart as an SVG, and as a PNG of 1600 by 900 pixels at 200 dpi; the figure is 8 by 4.5 inches. Lalitha's export(fig, folder, name) saves <name>.png and <name>.svg in folder. Her PNG is 1600 by 900 pixels, but lab image says 100 dpi and the paper says its text is half the size of the proof; her SVG is 1152 by 648 points, and the page wants 576 by 324.

    Fix export so it saves both files from the figure as it is, without changing the figure's size: <name>.svg, the figure's own size in points (72 an inch), and <name>.png at 200 dpi. The Check exports an 8 by 4.5 inch figure (1600 by 900 pixels, 576 by 324 points) and a 6 by 4 inch one (1200 by 800 pixels, 432 by 288 points) into folders of its own, and checks the figure's size afterwards.

    Tanvir will ask: the PNG has the pixels the paper asked for, so why is its text too small? What do figsize and dpi each change?

What you will learn

  • Questions first: decide what each panel answers before drawing anything.
  • A mosaic of chart functions: each panel is a function that draws on the Axes it is given.
  • Reshape, then draw: one shaping step per panel, all from the same rows.
  • Titles that state findings: computed from the data with f-strings, never typed.
  • Fit and save: constrained layout, a figure title, a fixed size.
  • A chart to publish: the sentence, the encoding, no furniture, direct labels, annotation.
  • Export: SVG, and a PNG at the dpi the page asks for.

More in Real work

Build "Capstone: Ira's dashboard and a chart to publish" 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.