Matplotlib · Chapter 10 of 16
Colour and colormaps
By the end of this chapter you can name a colour three ways and say where a chart's colours come from, choose a colormap from the right family (sequential, diverging or qualitative), say why viridis is the default, draw a grid as a heatmap with ax.imshow and its colorbar, centre a diverging map on a value that means something, colour by bands, and make one line stand out among grey ones. 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
Ira wants Delhi's whole year in one picture for the societies' report: which hours of which months are worst. That is 12 months by 24 hours, 288 numbers, and a line per month would be 12 lines on one chart. A heatmap puts each number in a cell and shows its size as a colour, so the colours have to be chosen well. Jaspreet's first try used the rainbow colormap jet: readers asked about "sharp edges" that are not in the data, and the printed newsletter, in grey, turned the worst and the cleanest hours into nearly the same dark grey. His map against India's standard of 60 µg/m³ came out with white at 126, so a reader took 126 as "fine". And Ira's city report has five lines in five colours, and everyone asks which one is Delhi.
Colour so far has been Matplotlib's: C0 for the first line, C1 for the next, viridis for chapter 5's colorbar. This chapter chooses it, because on a chart colour is data.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
hour_month.py: hour by month in viridis
Write hour_month_map(grid, months) in hour_month.py. grid is a NumPy array with one row per month and one column per hour of the day (0 to 23): Delhi's mean PM2.5 for that month and hour, from delhi_hourly.csv. months is a list of the rows' short names, in order ("Oct", "Nov", ...). It makes a new Figure and Axes and returns the Figure.
On the Axes: grid drawn as a heatmap (an image), unchanged and not turned (row 0 is the first month), with the colormap "viridis" and aspect="auto", so the cells stretch to fill the Axes. Beside it, a colorbar for that heatmap, labelled PM_LABEL (PM2.5 (µg/m³), kept in the starter). The y axis has one tick per row, labelled with that row's month from months, in order.
The Check calls it with all 12 months (October 2025 to September 2026) and with November to January (3 rows), and reads the image's data, its colormap, the aspect, the colorbar and its label, and the y tick labels. The Figure has one chart Axes; the colorbar's own Axes does not count.
- Task 2
against_sixty.py: station by month against 60
Write against_sixty(grid, sites) in against_sixty.py. grid is a NumPy array with one row per station and one column per month (October 2025 to September 2026): the station's mean PM2.5 that month, from daily.csv, and NaN for a month before the station was installed. sites is a list of the rows' site names, in order. It makes a new Figure and Axes and returns the Figure.
On the Axes: grid drawn as a heatmap, unchanged, in the diverging colormap "RdBu_r" (blue for low, white in the middle, red for high), with 60 (India's 24-hour standard) exactly in the middle of its colours, and every value of the grid inside the colours' range, none clipped. Beside it, a colorbar for that heatmap, labelled PM_LABEL (PM2.5 (µg/m³)). The y axis has one tick per row, labelled with that row's site, in order.
The Check calls it with Delhi's 22 stations and Gurugram's 8, and reads the image's data, its colormap, where 60 and the lowest and highest values fall in the colours, the colorbar and its label, and the y tick labels. aspect="auto" is a good idea; the Check does not need it.
- Task 3
one_city.py: one city in colour
Write one_city(months, city_means, city) in one_city.py. months is a list of 12 short month names ("Oct" to "Sep"), city_means a dict of city name to a list of its 12 monthly mean PM2.5 values (from city_daily.csv, in the same order as months), and city the name of the one to show. It makes a new Figure and Axes and returns the Figure.
On the Axes: one line per city in city_means, its means against months. Every line is in the colour GREY (#bbbbbb) except city's, which is in HIGHLIGHT ("C3"); both are kept in the starter. city's line is on top of the grey ones (drawn after them, or with a higher zorder). The title is the city's name and the words against the other cities: one_city(months, city_means, "Delhi") is titled Delhi against the other cities.
The Check calls it with Delhi among all five cities and with Gurugram among Noida, Gurugram and Faridabad, and reads every line's data and colour, which line is on top, and the title. A thicker highlighted line is welcome; the Check does not need it.
- Task 4 · interview question
Interview: no_jet.py, why not jet
Ira's day_hour_map(grid) draws a heatmap of Delhi's PM2.5, one row per day and one column per hour (a NumPy array from delhi_hourly.csv), with a colorbar labelled PM_LABEL, and returns the Figure. It uses the colormap "jet". Readers of the societies' newsletter ask about the sharp edges in its bright cyan and yellow bands, which are not in the data, and the newsletter is printed in grey, where the worst hours and the cleanest come out nearly the same dark grey.
Fix it: draw the grid, unchanged, in a colormap whose brightness rises evenly from its lowest colour to its highest. The Check accepts "viridis", "plasma", "inferno", "magma" or "cividis", not reversed (no _r). It still returns a Figure with one Axes holding the heatmap and a colorbar for it labelled PM_LABEL (PM2.5 (µg/m³)).
The Check calls it with the Diwali fortnight (15 to 26 October 2025, 12 rows) and with 1 to 7 January 2026 (7 rows), and reads the image's data, its colormap and the colorbar.
Tanvir will ask: why is viridis the default and not jet, and how would you check a colormap before you use it?
What you will learn
- Colours by name: "tab:orange", "#ff7d0c", "0.7", and the cycle "C0" to "C9" (rcParams["axes.prop_cycle"]).
- Colormaps and their three families: sequential (viridis, magma), diverging (RdBu_r), qualitative (tab10).
- Why viridis: its brightness rises evenly; jet's does not.
- ax.imshow(grid, cmap=, aspect="auto") and fig.colorbar: a heatmap and its key; ax.pcolormesh in one line.
- Norms: vmin/vmax, TwoSlopeNorm(vcenter=60), BoundaryNorm with a ListedColormap for bands.
- One colour, the rest grey: the best palette is often one colour.
More in Reading the numbers
Build "Colour and colormaps" 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.