Skip to content
Tutorials Center
My learning

Topics

Lesson 20 of 31 ยท Data Preparation and Design

Color, Labels and Accessibility

Choose sequential, diverging and categorical palettes, test contrast and color-blind safety, and make Plotly, Bokeh and Altair charts readable for everyone.

Color is the most powerful encoding in a chart and the easiest to misuse. The wrong palette can hide a pattern, invent one, or make the chart unreadable for the roughly one in twelve men and one in two hundred women who have some form of color-vision deficiency. Good color choices are mostly a matter of a few rules, and those rules apply identically in Plotly, Bokeh and Altair.

This lesson covers the three kinds of palette, how to check contrast, how to design so that color is never the only signal, and what you can do for screen-reader and keyboard users. The libraries' own color calls are read-only here, because they need Python and a browser display. The runnable examples are stand-ins: small JavaScript programs that compute the same measurements (contrast ratio, lightness) that accessibility tools use, so you can test any color pair yourself. If you want to see how a chart maps data to color in the first place, read How a Chart Works Inside.

Match the palette to the data

Pick the palette by asking what kind of values the color represents.

Data Palette type Example
Ordered numbers, low to high Sequential: one hue, light to dark Sales per region
Numbers with a meaningful middle (zero, target, average) Diverging: two hues meeting at a neutral Profit and loss, change from baseline
Unordered groups Categorical: distinct hues Product lines, countries

Using a categorical palette for ordered values (a rainbow for temperature) forces readers to memorize which color is "more". Using a sequential palette for groups suggests a ranking that does not exist.

Python
import plotly.express as px

# Sequential: continuous color scale
px.scatter(df, x="x", y="y", color="sales", color_continuous_scale="Viridis")

# Diverging: center the scale on zero
px.imshow(change, color_continuous_scale="RdBu", color_continuous_midpoint=0)

# Categorical: a discrete sequence of colors
px.bar(df, x="month", y="sales", color="product",
       color_discrete_sequence=px.colors.qualitative.Safe)

Read-only Plotly. In Altair the equivalent is alt.Color("sales:Q", scale=alt.Scale(scheme="viridis")); in Bokeh it is linear_cmap with a palette such as Viridis256.

The diverging example shows why the midpoint matters. Without color_continuous_midpoint=0, the neutral color lands in the middle of the data range instead of at zero, and a small positive number can look like a loss.

Why rainbow scales mislead

The rainbow (or "jet") scale seems vivid, but its lightness goes up and down: yellow is bright, blue is dark, and the brightness peaks in the middle. Your eye reads the bright bands as boundaries or hot spots that are not in the data. A good sequential scale gets steadily darker (or lighter) from one end to the other, which is called being perceptually ordered. Viridis was designed for exactly this, and it also stays readable for most color-blind viewers and in grayscale print.

flowchart TD
    A["What do the values mean?"] --> B{"Ordered numbers?"}
    B -- "No, just groups" --> C["Categorical<br/>distinct hues, 8 or fewer"]
    B -- "Yes" --> D{"Meaningful midpoint?"}
    D -- "No" --> E["Sequential<br/>one hue, light to dark"]
    D -- "Yes" --> F["Diverging<br/>two hues, neutral center"]

A three-question decision for the palette type.

Measuring contrast

Contrast is the difference in brightness between two colors. The WCAG guidelines (Web Content Accessibility Guidelines) define a contrast ratio from 1:1 (identical) to 21:1 (black on white), and set minimums that apply directly to charts:

  • 4.5:1 for normal-sized text such as tick labels and annotations.
  • 3:1 for large text and for meaningful graphics: lines, bars, markers and chart borders that carry information, measured against the background next to them.

The formula converts each color to relative luminance (brightness as the eye perceives it) and compares the lighter and darker value. The program below does that. Change the pairs and see which of your own colors pass.

JS WCAG contrast ratio checker

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Look at the middle rows. Gray #767676 is the lightest gray that passes 4.5:1 on white, and #777777 just misses; the eye cannot see that difference, but the rule can. The yellow from a popular color-blind-safe palette is a classic trap: it is fine as a fill next to a dark color, but as a thin line on a white background it scores about 1.3 and nearly disappears. In practice this means thin lines need darker colors than large filled areas, and you should add a visible outline or switch the background when using light hues.

Lightness check for sequential ramps and categorical sets

Contrast against the background is only half of it. You also want the steps of a sequential ramp to differ in lightness, and the colors of a categorical set to be distinguishable even when hue is hard to see.

JS Is the ramp ordered? Which colors look alike in grayscale?

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The blue ramp passes: each step is darker than the one before, so the order survives a grayscale print. The rainbow does not: its lightness jumps up, down and up again. The eight colors in this palette (designed by Okabe and Ito for color-blind readers) differ strongly in hue, yet orange and sky blue have almost the same lightness. That is acceptable only if something else separates them, which brings us to the next rule.

Never use color alone

About 8% of men cannot reliably tell red from green, and many more people print in grayscale, view a projector washed out by daylight, or simply look at a small screen. Add a second signal wherever color carries meaning:

  • Direct labels. Write the series name at the end of its line instead of (or as well as) a legend. The reader does not have to match colors across the page. In Altair add a mark_text layer at the last point; in Plotly use an annotation or a text trace; in Bokeh use a Label.
  • Shape and line style. Give series different marker symbols (symbol= in Plotly, shape= in Altair) or dash patterns.
  • Position and order. Sort bars and place related series next to each other.
  • Limit the count. More than about eight categories cannot be told apart by color. Group the small ones into "Other", or use small multiples instead of one crowded chart (see Layering, Faceting and Concatenation).

You can see the check in practice with a small page that tests swatches. This is an HTML stand-in: it lists each palette color with its contrast against a white background and picks black or white text for the label.

Swatches with contrast badges

Make charts available to assistive technology

Interactive charts in all three libraries are drawn as SVG or canvas graphics. A screen reader sees a picture with no meaning unless you supply one. What you can do depends on the library, so stay conservative and add the information around the chart where the library offers no hook.

  • Give the chart a text alternative. Wrap the chart in a <figure> with a <figcaption> that states the finding ("Sales rose every quarter, from 12k to 31k"). If you embed with HTML, role="img" and an aria-label on the container give the whole chart a name. Vega-Lite has a description property on the spec, and the Vega runtime uses it as the accessible label; check that your version of vega-embed does this before relying on it.
  • Offer the data as a table. A visible or collapsible <table> under the chart gives screen-reader users and keyboard users exact values, and helps everyone who wants to copy a number. It is the most reliable fallback.
  • Do not hide information in hover. Tooltips need a mouse. If a number matters, label it directly or put it in the table. Do not expect hover or drag zoom to work from the keyboard unless you have tested it.
  • Respect user preferences. Avoid animation that loops, and use text sizes of at least 12 px. Use the browser's prefers-color-scheme and prefers-reduced-motion settings in the page around the chart.

Treat accessibility as testing, not guessing: tab through the page, try a screen reader, and run your colors through a contrast checker like the one above.

What is real and what is the stand-in

  • Real, read-only: the Plotly, Altair and Bokeh calls and the HTML around a published chart. They need Python and a browser display.
  • Stand-in: the contrast and lightness programs. The math is the same math that the WCAG formula and accessibility audit tools use. What they do not do is simulate every kind of color-vision deficiency, so use a dedicated simulator in addition when you design a final palette.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. You are plotting profit and loss relative to zero. Which palette type fits?

    Values with a meaningful midpoint need a diverging scale, centered on that midpoint (in Plotly, color_continuous_midpoint=0).
  2. 2. What is the minimum WCAG contrast ratio for normal-sized text such as tick labels?

    Normal text needs 4.5:1. Large text and meaningful graphical objects such as lines and bars need 3:1.
  3. 3. Why is a rainbow color scale a poor choice for ordered numbers?

    A good sequential scale changes steadily in lightness. The rainbow does not, so readers see false edges and cannot order the colors.
  4. 4. Which change helps a color-blind reader most?

    A second signal such as a direct label, shape or dash pattern means the meaning survives even when two colors look alike.

Summary

  • Choose sequential palettes for ordered values, diverging for values with a midpoint, and categorical for unordered groups, with about eight colors at most.
  • Avoid rainbow scales; prefer perceptually ordered ones such as Viridis, and center diverging scales on the real midpoint.
  • Test contrast: 4.5:1 for text, 3:1 for lines, bars and markers against their background.
  • Never let color be the only signal: add direct labels, shapes, dash styles and sorted order.
  • Give each chart a text alternative and offer the data as a table; do not hide key values in hover tooltips.
  • The color calls are read-only here; the JavaScript stand-ins compute the same contrast and lightness numbers.

Next: Storytelling with Charts.