Matplotlib · Chapter 14 of 16
Plotting from pandas
By the end of this chapter you can draw a pandas Series or DataFrame with its own .plot, choose the kind of chart (kind= or .plot.bar, .plot.scatter and the rest), send the drawing into an Axes you made with ax=, and shape a table first (pivot, groupby) so that one call draws what you mean. 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
Every SaafHawa table already lives in pandas: Lalitha's months, the cities' days, Jaspreet's Diwali week. For a chart of the five cities' year you have done it the long way: a mask for each city, five ax.plot calls, five label= arguments and a legend. That is five chances to give Noida's line Gurugram's name. Ira sends one of these charts to the housing societies every month, and Lalitha wants the same chart for PM10, for the weather, for the investors' months.
pandas has a .plot method on every Series and DataFrame that does this in one call: the index goes along x, each column becomes a line, and the legend comes free. It draws with Matplotlib, so everything you know still works on what it gives back. Tanvir has one warning: it is easy to let pandas draw on a figure you never meant, and then the chart you send is empty.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
city_year.py: five cities' year in one call
Write city_year(city_days) in city_year.py. city_days is city_daily.csv as read_csv reads it, with its date column turned into dates by pd.to_datetime: a long table, one row per city per day.
Pivot it into a wide table (the dates as the index, one column per city, the pm25 values in the cells), make a Figure and its one Axes with plt.subplots(), and draw the wide table into that Axes with one call of its plot method, titled in the same call PM2.5 in five cities, 2025-26 (exactly). Label the y axis PM2.5 (µg/m³), which the starter keeps in Y_LABEL, and return the Figure.
The Figure has one Axes with exactly one line per city (the dates along x, that city's PM2.5 up y) and a legend with the city names. The Check tries the whole year of five cities, and Delhi and Noida in November 2025 (two lines). Only your Figure may be open after the call: a plot call that makes a figure of its own fails.
- Task 2
month_bars.py: the months as bars, the worst first
Write month_bars(days, city) in month_bars.py. days is one city's rows of city_daily.csv as read_csv reads it, with one more column, month: the date's first seven characters, like 2025-10.
Work out the mean pm25 of each month (group the rows by month), sort the means from the highest to the lowest, and draw them as vertical bars with the Series' own plot, into the Axes of a Figure you make with plt.subplots(). Title it <city>, month by month (Delhi, month by month for Delhi), label the y axis PM2.5 (µg/m³) (Y_LABEL in the starter), and return the Figure.
The Figure has one Axes with one bar per month, each labelled with its month (2025-11), the highest mean on the left. The Check tries Delhi's whole year and Gurugram's October to December 2025.
- Task 3
wind_scatter.py: PM2.5 against wind, from the table
Write wind_scatter(days, city) in wind_scatter.py. days is one city's rows of city_daily.csv as read_csv reads it.
Make a Figure and its one Axes with plt.subplots(), then draw a scatter with the DataFrame's own plot into that Axes: the column wind_kmh along x and the column pm25 up y, one point per day. The words, exactly:
title: PM2.5 against wind in <city> (PM2.5 against wind in Delhi)
x axis label: Wind (km/h)
y axis label: PM2.5 (µg/m³), which the starter keeps in Y_LABEL
Return the Figure, with one Axes holding one scatter. The Check tries Ghaziabad's whole year and Delhi's July 2026.
- Task 4 · interview question
Interview: diwali_report.py, the report with empty panels
Jaspreet's diwali_report(wide, peaks) lays out a mosaic of two panels, week on the left and peak on the right, and fills each with one pandas call. wide is diwali_hourly.csv pivoted: the hours (as dates) in the index, one column per sensor's site, its PM2.5 in the cells. peaks is each site's highest hour (wide's maximum of each column), sorted. The Figure it returns has two empty panels, and two more figures are left open with the lines and the bars in them.
Fix it so the Figure it returns has two Axes: on the left one line per column of wide, titled Six sensors, Diwali week, with the y axis labelled PM2.5 (µg/m³) (Y_LABEL); on the right horizontal bars of peaks, one per site, in peaks' order from the bottom, titled Each sensor's highest hour. No other figure may be left open, and every pandas plot call in the file must name the panel it draws in. The Check tries the six sensors' whole week, and three sensors over 20 and 21 October.
Tanvir will ask: why did the DataFrame's plot make a new figure instead of drawing in the mosaic's panel, and where did the bars go?
What you will learn
- Series.plot() and DataFrame.plot(): one call draws the table, and returns an Axes.
- The index is x, each column a line: and the legend names the columns.
- pivot and groupby: shape the table first, so the columns are the lines you want.
- kind= and the accessors: .plot.line, .plot.bar, .plot.barh, .plot.hist, .plot.box, .plot.scatter(x=, y=), .plot.area.
- title=, xlabel=, ylabel= in the call; the Axes' methods for everything else.
- ax=: draw into your own Axes, a panel of a mosaic included.
- subplots=True: one Axes per column.
More in Real work
Build "Plotting from pandas" 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.