Colour is the most powerful and the most misused tool in visualization. Used well it highlights and groups; used badly it confuses and excludes.
Colour Properties
A colour has hue (the kind: red, blue), saturation (intensity or purity) and lightness (how light or dark). Hue is good for separating categories, whereas lightness is the best channel to show order and amount.
Three Kinds of Palette
- Sequential palette: one hue running from light to dark, for ordered data from low to high, such as population density. Light means low and dark means high.
- Diverging palette: two hues meeting at a neutral midpoint, for data with a meaningful centre, such as profit and loss, or above and below average.
- Categorical (qualitative) palette: distinct hues of similar lightness for unordered categories. Limit it to about five or six colours; more cannot be told apart or remembered.
Colour Rules
Use colour sparingly and with meaning, for example grey for context and one strong colour for emphasis. Keep the same colour for the same category on every chart. Respect cultural and conventional meanings (red for danger or loss, green for good, with care). Do not use a rainbow palette for ordered data, because its hues do not look ordered and it hides the detail.
Accessibility and Contrast
About 8 percent of men and 0.5 percent of women have colour vision deficiency, most often red-green. Do not use colour alone to carry meaning: add labels, patterns or shapes, and choose safe palettes (blue and orange). The readability of text depends on its contrast ratio with the background. The Web Content Accessibility Guidelines (WCAG) require at least 4.5 : 1 for normal text and 3 : 1 for large text.
Definition: Contrast ratio. A number from 1 to 21 comparing the relative luminance of two colours as (L1 + 0.05) / (L2 + 0.05), where L1 is the lighter; WCAG requires at least 4.5 for normal text.
Table 4.3: Types of colour palette
| Palette | Data | Example | Rule |
|---|---|---|---|
| Sequential | Ordered, low to high | Population density | Light to dark of one hue |
| Diverging | Has a midpoint | Profit and loss | Two hues, neutral centre |
| Categorical | Unordered groups | Product lines | Five or six distinct hues |
Example 4.3: Checking text contrast
Problem: Check the contrast ratio of black (#000000), grey (#767676) and yellow (#FFFF00) text on a white background against the WCAG minimum of 4.5.
Solution:
- Relative luminance of white is 1. Black has luminance 0, so the ratio is (1 + 0.05) / (0 + 0.05) = 21.
- Grey #767676 has luminance about 0.181, giving (1.05) / (0.231) = 4.54, which just passes.
- Yellow #FFFF00 has luminance about 0.93, giving 1.05 / 0.98 = 1.07, which fails badly.
- So yellow text on white is unreadable, while the grey is the lightest grey that is acceptable.
Result: Contrast ratios of 21, 4.54 and 1.07; the yellow fails the 4.5 requirement.
def lum(hexcolor):
c = [int(hexcolor[i:i + 2], 16) / 255 for i in (1, 3, 5)]
c = [v / 12.92 if v <= 0.03928 else ((v + 0.055) / 1.055) ** 2.4
for v in c]
return 0.2126 * c[0] + 0.7152 * c[1] + 0.0722 * c[2]
def contrast(a, b):
hi, lo = sorted((lum(a), lum(b)), reverse=True)
return (hi + 0.05) / (lo + 0.05)
for col in ("#000000", "#767676", "#ffff00"):
r = contrast(col, "#ffffff")
print(col, round(r, 2), "pass" if r >= 4.5 else "fail")
Output
#000000 21.0 pass
#767676 4.54 pass
#ffff00 1.07 fail
Common mistakes
- Using a rainbow palette for ordered data.
- Using red and green as the only difference between two series.
- Using more than six categorical colours.
- Placing light grey text on a white background.
Key takeaways
- Hue, saturation, lightness: lightness shows order best.
- Sequential, diverging and categorical palettes.
- About 8 percent of men are colour blind; add labels or patterns.
- WCAG contrast: at least 4.5 for normal text.
Practice questions
- When is a diverging palette used?
- How many categorical colours should be used at most?
- What is the minimum WCAG contrast ratio for normal text?
Answers to the practice questions
- For data with a meaningful midpoint, such as profit and loss.
- About five or six.
- 4.5 : 1.
Sources
- Official BCA syllabus Data Visualization, course code 26BCA404DSE11, Unit IV
