Colour
Last updated
Colour is the easiest thing to add to a chart and the easiest to get wrong. It can show order, show a middle point, tell groups apart, or point at one thing, and each job needs a different kind of palette. In this lesson you'll learn the three kinds, see a rainbow colour map invent an edge that isn't in the data, check a chart as people with colour blindness see it, and use one colour to make a point.
By the end of this lesson you can
- Choose a sequential, diverging or categorical palette to match the data
- Explain why perceptually uniform maps such as viridis beat rainbow maps
- Check a chart for colour-blind readers and not rely on colour alone
- Use grey plus one highlight colour to make a point
Three kinds of palette
A palette (or, for continuous numbers, a colour map) is the set of colours a chart uses. Which kind to use depends on the data:
| Kind | For | Looks like | matplotlib maps |
|---|---|---|---|
| Sequential | numbers from low to high | one hue, light to dark | "Blues", "viridis" |
| Diverging | numbers above and below a middle that matters (zero, an average) | two hues, with a pale middle | "RdBu", "PuOr" |
| Categorical | groups with no order | different hues, about equally strong | "tab10" (the default), "Set2" |
Run the cells: rain in each Tindahan town for eight weeks, as colours.
ax.imshow(rain, cmap="Blues")draws the table as a grid of coloured squares, a heat map. Rain only goes from none to lots, so it's sequential: more rain, darker blue.aspect="auto"lets the squares stretch to fill the chart.fig.colorbar(im, ax=ax)adds the key that turns a colour back into a number. A heat map without one can't be read.- The third cell asks a different question: was each week wetter or drier than that town's usual week? Now there's a middle that matters, zero, so the map is diverging: blue wetter, red drier, pale near usual.
vmin=-biggest, vmax=biggestputs zero exactly at the pale middle. Without it, the middle colour would land wherever the data's halfway point happened to be.
Wrong way: the rainbow
For years matplotlib's default colour map was jet, a rainbow from dark blue to dark red, and you'll still see it everywhere. Here's a smooth hill of numbers, with no edges anywhere, drawn with jet and with viridis, the map that replaced it as the default in matplotlib 2.0. Run the cells.
In the jet chart you see rings: a bright yellow band, sharp-looking edges between cyan and yellow. None of them are in the data; the hill rises smoothly. The viridis chart shows what's really there, a smooth rise.
The third cell explains it. It prints how bright each of nine colours along each map is, from 0 (black) to 1 (white). Viridis gets brighter at every step, just as the numbers get bigger. Jet shoots up to bright yellow-green in the middle, then falls back to dark red. Your eye reads brightness as "more", so jet says the middle of the range is the highest, and puts edges where the brightness changes fast. Step through the same thing as a picture:
jet, the old rainbow. Bottom: viridis.The check: for numbers in order, use a map whose brightness changes steadily in one direction: viridis, cividis, or a single hue like "Blues". Print it in grey in your head; if low and high could be confused, or a band jumps out, change it.
Colour for everyone
About one man in twelve and one woman in two hundred has some colour vision deficiency, most often trouble telling red from green. A chart that only works in full colour shuts them out. Try three palettes, and see each as people with each kind of colour blindness would.
Red, green, orange and brown: hard to tell apart with the two commonest kinds.
With the red-green palette, Lipa's green and Tanauan's orange turn into almost the same olive for protanopia and deuteranopia, and their lines cross, so the legend can't help. Okabe and Ito's palette was designed to stay distinguishable for all three kinds. The highlight palette doesn't rely on telling hues apart at all: names are written on the lines, and colour only says "this one".
Rules that follow:
- Never let colour be the only way to tell things apart. Label lines and bars directly (6.6), or use dashes and markers as well.
- Avoid red against green as the main contrast.
- Prefer a colour-blind-safe set when you need several hues: matplotlib's default
tab10is fair, Okabe–Ito is better, andviridisandcividiswork for everyone.
One colour, one point
Often a chart has one thing to say. Then the strongest use of colour is the simplest: everything grey, and the one thing that matters in a highlight colour. Run the cells: the average day's sales across all four stores, by day of the week.
The second cell draws every bar the same blue, and the reader has to find the point. The third makes six bars grey and Saturday pink, and the title says what the pink means. Saturday averages 226.9 kg, half as much again as Tuesday, the quietest day, at 149.4 kg. That's the weekly bump from lesson 6.3.
The grey bars aren't wasted: they're the context that makes Saturday look high. One highlight works because there's only one. Two or three can work; five highlights is no highlight.
Check what you learned
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- matplotlib: Choosing Colormaps in Matplotlib: sequential, diverging and qualitative maps, and their lightness
- Crameri, Shephard and Heron, The misuse of colour in science communication, Nature Communications 11, 2020: how rainbow maps distort data and shut out colour-blind readers
- Borland and Taylor, Rainbow Color Map (Still) Considered Harmful, IEEE Computer Graphics and Applications 27(2), 2007: false edges and the lightness problem
- Machado, Oliveira and Fernandes, A Physiologically-based Model for Simulation of Color Vision Deficiency, IEEE TVCG 15(6), 2009: the simulation used in the colour-blind preview
- matplotlib: matplotlib.axes.Axes.imshow: cmap, vmin, vmax and aspect