Introduction
Colour is the most eye-catching part of a chart and the easiest to get wrong. Used well, it groups related items and highlights the one that matters. Used badly, it suggests an order that does not exist, hides information from readers with colour-vision deficiency, or makes text unreadable. Good colour choices follow from the data and can be checked with simple calculations.
How it works
Three kinds of palette.
- A categorical palette uses clearly different hues for unordered groups. Keep it to about six to eight colours; beyond that, readers cannot match colours to the legend, so group the rest into Other or label directly.
- A sequential palette goes from light to dark in one hue for ordered values, such as income or temperature. Darker usually means more.
- A diverging palette uses two hues meeting at a neutral middle, for values above and below a meaningful reference, such as profit and loss around zero.
Colour-vision deficiency. About 1 in 12 men and 1 in 200 women have some colour-vision deficiency, most often difficulty telling red from green. Avoid red–green pairs as the only difference, prefer palettes designed to be safe (blue–orange works well), and vary lightness as well as hue so the chart still works in greyscale. Most importantly, never rely on colour alone: label lines directly, use patterns or shapes, and put the key values in text.
Contrast. The Web Content Accessibility Guidelines (WCAG) define a colour's relative luminance L from its red, green and blue channels: each channel is scaled to 0–1, linearised (c ÷ 12.92 for small values, ((c + 0.055) ÷ 1.055)^2.4 otherwise) and weighted 0.2126, 0.7152 and 0.0722, because the eye is most sensitive to green. The contrast ratio is (L1 + 0.05) ÷ (L2 + 0.05) with L1 the lighter colour. Black on white is 21:1; identical colours are 1:1. Normal text needs at least 4.5:1 (level AA), and chart elements such as bars and lines at least 3:1 against their background.
Building a sequential palette. Colours are written in hex, #RRGGBB, two hexadecimal digits per channel. Interpolating each channel linearly between a light and a dark colour gives evenly spaced steps; specialised tools interpolate in perceptual colour spaces for even smoother results.
Example
# WCAG contrast ratio between two colours, from their relative luminance.
def channel(c):
c = c / 255
return c / 12.92 if c <= 0.04045 else round(((c + 0.055) / 1.055) ** 2.4, 12)
def luminance(hex_colour):
r, g, b = (int(hex_colour[i:i + 2], 16) for i in (1, 3, 5))
return 0.2126 * channel(r) + 0.7152 * channel(g) + 0.0722 * channel(b)
for fg, bg in [("#000000", "#FFFFFF"), ("#777777", "#FFFFFF"), ("#C16B3C", "#EDE9DF")]:
l1, l2 = sorted([luminance(fg), luminance(bg)], reverse=True)
print(fg, "on", bg, "ratio", round((l1 + 0.05) / (l2 + 0.05), 2))Line by line
channellinearises one 0–255 channel, as WCAG specifies.luminancereads the hex pairs, converts them from base 16 and weights the channels.- The ratio puts the lighter luminance on top; black on white gives the maximum, 21.
- #777777 on white just misses the 4.5:1 needed for normal text.
Expected result
#000000 on #FFFFFF ratio 21.0
#777777 on #FFFFFF ratio 4.48
#C16B3C on #EDE9DF ratio 3.19
In the real world
Government and company sites must meet WCAG contrast rules by law in many countries. News graphics teams publish colour guides with tested palettes, and tools such as ColorBrewer offer sequential, diverging and categorical palettes that are safe for colour-vision deficiency and print.
Common mistakes
- Using a rainbow palette for ordered data, which has no natural order of hues.
- Distinguishing series by red and green only.
- Light grey text on white that fails the contrast ratio.
- Too many categorical colours for readers to match with a legend.
Best practices
- Pick the palette type from the data first, then the colours.
- Check your chart in greyscale and with a colour-blindness simulator.
- Label directly and use colour to highlight one or two key items.
Summary
Categorical palettes separate groups, sequential palettes show order and diverging palettes show distance from a midpoint. Design for colour-vision deficiency by varying lightness and never relying on colour alone, and check WCAG contrast: 4.5:1 for text and 3:1 for chart marks.
Quick check
Key takeaways
- Match the palette to the data: categories, ordered values, or values around a midpoint.
- Never encode meaning with colour alone; add labels, patterns or position.
- Text needs a contrast ratio of at least 4.5:1, chart marks at least 3:1.
Practice problems for this lesson
My notes
Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.