Skip to content
Tutorials Center
My learning

Topics

Lesson 15 of 44 ยท Scales and Axes

Color Scales, Interpolators and Palettes in D3

Color data with D3: scaleSequential and interpolateViridis, diverging scales, quantize, quantile and threshold buckets, categorical schemes.

Color is a data channel as powerful as position. D3 treats it with the same scale idea: a domain of data values maps to a range of colors. Two modules help: d3-scale supplies the mapping, and d3-scale-chromatic supplies well-designed palettes and interpolators such as Viridis and Blues. The d3 bundle includes both, and d3-color and d3-interpolate handle the arithmetic of colors.

Sequential scales and interpolators

An interpolator is a function from t in [0, 1] to a color: d3.interpolateBlues(0) is a light blue and d3.interpolateBlues(1) a dark blue. d3.scaleSequential wraps an interpolator and maps your domain onto [0, 1].

JavaScript
const color = d3.scaleSequential()
  .domain([0, 100])
  .interpolator(d3.interpolateYlGnBu);

color(0);    // a pale yellow
color(100);  // a deep blue

There are many sequential interpolators: interpolateBlues, Greens, Oranges, Purples, Reds, Greys, plus multi-hue ones like interpolateViridis, Plasma, Inferno, Magma, Cividis, Turbo and YlGnBu. Viridis and Cividis are designed to be perceptually uniform and readable by people with color-vision deficiency, so they are great defaults for heatmaps.

A heatmap with scaleSequential

Diverging scales

When data has a meaningful midpoint (zero change, the average, a target), use a diverging scale: two hues meet at a neutral center. d3.scaleDiverging takes a domain of three values [min, mid, max].

JavaScript
const color = d3.scaleDiverging()
  .domain([-10, 0, 10])
  .interpolator(d3.interpolateRdBu);   // red negative, blue positive

Other diverging interpolators: PiYG, PRGn, BrBG, PuOr, RdYlBu, RdYlGn, Spectral. Do not use a rainbow (interpolateRainbow) to show ordered values; its hue jumps look like categories and it is not perceptually even.

Quantize, quantile and threshold: discrete buckets

Sometimes you want a few distinct colors instead of a smooth gradient, as in a choropleth map with a 5-step legend.

Scale How it splits the domain
scaleQuantize Equal-width intervals of the domain
scaleQuantile Equal-count groups (each color has about the same number of items)
scaleThreshold Intervals at cut points you choose (n cut points give n+1 colors)
JavaScript
const q = d3.scaleQuantize([0, 100], d3.schemeBlues[5]);
q(12);  // first bucket color
q(97);  // last bucket color
q.invertExtent(q(12));  // [0, 20]  the interval for that color

schemeBlues[k] is an array of k colors for k from 3 to 9. All the sequential and diverging palettes come in this form, e.g. d3.schemeRdBu[7].

Continuous versus bucketed colors

Quantile classes suit skewed data: they put roughly the same number of items in each class, so you see variation even when a few values are huge.

Categorical palettes

For categories use ordinal scales with a categorical scheme (see Ordinal Scales):

  • d3.schemeCategory10: the classic 10 colors.
  • d3.schemeTableau10: a modern 10-color set.
  • d3.schemeSet1, schemeSet2, schemeSet3, schemePaired, schemeAccent, schemeDark2, schemePastel1/2.

Keep to 10 colors or fewer. Beyond that, color alone cannot be told apart, and you should add direct labels or interaction.

Working with colors directly: d3.color

d3.color(string) parses any CSS color into an object you can inspect and modify:

JavaScript
const c = d3.color("steelblue");  // {r: 70, g: 130, b: 180, opacity: 1}
c.brighter(1);                    // lighter
c.darker(0.5);                    // darker
c.formatHex();                    // "#4682b4"
c.copy({ opacity: 0.4 });         // a new color with opacity

Also available: d3.rgb, d3.hsl, d3.lab, d3.hcl, d3.cubehelix. Working in hcl or lab gives perceptually uniform adjustments, so equal steps look equal.

d3.interpolate(a, b) returns a function of t that blends two values, and it picks the right method by type: numbers, colors, strings with numbers, arrays, objects, dates. Specific ones are d3.interpolateRgb, interpolateHsl, interpolateLab, interpolateHcl, interpolateNumber. d3.quantize(interpolator, n) samples an interpolator into an array of n values, which you can feed to an ordinal or threshold scale.

d3.color and interpolation

The top strip (RGB) darkens through purple (#800080 at the midpoint); the bottom (HCL) travels around the hue wheel through a bright pink (#f50086), so lightness stays more even. Pick the space that matches the look you want: RGB for the shortest straight blend, HCL or Lab for perceptual evenness.

Guidelines for color

  • Use sequential for ordered values from low to high, diverging when a midpoint matters, categorical for unordered groups.
  • Do not rely on color alone: add labels, patterns or position for accessibility.
  • Check contrast against your background, and in dark mode prefer lighter or more saturated colors.
  • Pick a color-blind-safe palette (Viridis, Cividis, Okabe-Ito style sets) when the audience is wide.

d3.scaleSequential clamps nothing by default; values outside the domain extrapolate and may produce invalid colors (rgb(NaN...)) for some interpolators. Call .clamp(true) when out-of-range data is possible.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is a D3 interpolator?

    Interpolators map a normalized parameter to values. scaleSequential maps data to t and calls the interpolator.
  2. 2. How many values does a diverging scale's domain need?

    scaleDiverging takes [min, mid, max]. The mid value maps to the neutral center of the palette.
  3. 3. Which scale puts roughly the same number of data points into each color class?

    Quantile scales split the sorted data into equal-count groups, which is good for skewed data.
  4. 4. Why avoid interpolateRainbow for ordered data?

    Rainbow palettes distort perception of magnitude. Viridis and single-hue ramps are better for ordered data.

Summary

  • scaleSequential and scaleDiverging map data to color interpolators; Viridis and Blues are good defaults.
  • scaleQuantize, scaleQuantile and scaleThreshold create discrete color buckets; schemeX[k] supplies k colors.
  • scaleOrdinal with schemeTableau10 or schemeCategory10 colors categories.
  • d3.color, d3.interpolate and d3.quantize give you full control; interpolate in Lab or HCL for even gradients.

Next: Axes: Ticks, Labels and Gridlines