Matplotlib · Chapter 6 of 16
Bar charts
By the end of this chapter you can compare categories with ax.bar and ax.barh, sort the bars so the ranking reads itself, draw grouped and stacked bars, write each bar's number on it with ax.bar_label, take away the furniture a chart does not need, and say why a bar must start at zero. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Reading the numbers
- 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 month Ira sends the housing societies a report, and it opens with one question: is our kind of site worse than the others? Every quarter Lalitha puts the cities, the seasons and the customers into the investors' deck. Jaspreet wants to know which sensors missed hours last month, so he knows which roofs to climb. Each of these is a handful of categories (kinds of site, cities, stations, months), each with one number.
A line between categories says something false: that the air goes "from office to roadside", as if there were a road between them. A scatter has no x to put a name on. What works is one bar per category, its length the number. A bar chart is the plainest chart there is, and the easiest to get wrong: the bars in the wrong order, the names unreadable, or the axis cut so that a 28 % difference looks like 166 %.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
city_bars.py: the cities by their air
Write city_bars(means) in city_bars.py. means is a pandas Series from city_daily.csv: one mean PM2.5 per city, its index the city names, in alphabetical order (as groupby gives it). It makes a new Figure and Axes and draws one set of horizontal bars, one bar per city, labelled with the city names (the names on the y axis), sorted by value: the cleanest city at the bottom, the worst at the top. It labels the x axis exactly Mean PM2.5 (µg/m³) (the starter keeps it in X_LABEL) and returns the Figure.
The Check reads the bars from bottom to top and compares their names, their order and their lengths with the Series it gave. It tries the whole year's means, November 2025's, and July 2026's for three cities only (Delhi, Gurugram and Noida).
- Task 2
uptime_bars.py: every bar says its number
Write uptime_bars(uptime) in uptime_bars.py. uptime is a pandas Series from daily.csv for one city's stations in one month: its index the sites (the names in stations.csv, such as Vaishali), its values each station's mean uptime_pct that month, rounded to one decimal. It makes a new Figure and Axes and:
draws one set of horizontal bars, one per site, labelled with the site names, in any order;
writes each bar's value at its end with ax.bar_label, with one decimal and a % sign (71.0%, 98.8%, 100.0%);
hides the top and the right spines of the Axes.
It returns the Figure. The Check reads every bar's name and length, the label on each bar and the two spines, and tries Ghaziabad in January 2026 (when Vaishali's router died), Noida in September 2026 and Faridabad in September 2026.
- Task 3
grouped.py: November against July, city by city
Write season_bars(cities, nov, jul) in grouped.py. The three are lists of the same length: city names, and each city's mean PM2.5 in November 2025 and in July 2026 from city_daily.csv (rounded to one decimal), in the same order. It makes a new Figure and Axes and draws grouped bars:
two sets of vertical bars (two calls of the Axes' bar method), November's and July's, one bar per city in each set, all standing on zero;
each city's two bars side by side and not overlapping, November on the left and July on the right;
one x tick under each pair, between its two bars, labelled with the city, in the order given (ax.set_xticks(positions, labels));
a legend whose two entries are exactly November and July.
It returns the Figure. The Check tries all five cities, then three of them in another order (Noida, Delhi, Gurugram).
- Task 4 · interview question
Interview: from_eighty.py, the chart that started at 80
Lalitha's clean_bars(cities, means) draws each city's mean PM2.5 as a bar standing on 80, so that the differences show: Gurugram's bar is a stub, and an investor asked why Ghaziabad is four times as polluted. cities and means are lists of the same length from city_daily.csv (each city's mean over the year, one decimal). Fix it so the Figure it returns has one Axes with one set of vertical bars in the order given, labelled with the cities, each bar as tall as its mean and standing on zero, and the y axis starting at zero. Keep the title Mean PM2.5 by city, 2025-26 and the y axis label PM2.5 (µg/m³) (Y_LABEL) as they are. The Check tries all five cities, then Gurugram and Ghaziabad alone.
Tanvir will ask: why must a bar start at zero, when a line chart may zoom in?
What you will learn
- ax.bar(names, values) and ax.barh: vertical and horizontal bars, one per category.
- Sorting: the order of the bars is the order you give them.
- Grouped bars: two sets side by side, with positions from np.arange, a width and ax.set_xticks(positions, labels).
- Stacked bars: bottom=, one set standing on another.
- The baseline is zero: a bar's length is its value.
- ax.bar_label and the spines: the numbers on the bars, the frame taken away.
- Why not a pie: what ax.pie makes hard.
More in Reading the numbers
Build "Bar 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.