Skip to content
Tutorials Center
My learning

Topics

Lesson 20 of 42 ยท Layout, Styling and Themes

Colors and Color Scales

Choose and customize color in Plotly: color formats, discrete sequences, color_discrete_map, continuous and diverging scales, reversed and custom scales, colorbars and colorblind-safe choices.

Color is the strongest visual signal in a chart, and the easiest to get wrong. Plotly handles two different jobs with color. Discrete colors tell categories apart (one color per species). Continuous colors encode a number (a temperature, a correlation) through a color scale, a smooth gradient with a color bar as a legend.

This lesson shows how to pick, override and build both kinds, and how to keep your charts readable for people with color-vision differences.

Color formats

Anywhere Plotly takes a color, you can use any of these:

Format Example
Hex "#636efa", "#f80"
RGB / RGBA "rgb(99,110,250)", "rgba(99,110,250,0.4)"
HSL / HSLA "hsl(235,95%,69%)"
CSS color name "crimson", "steelblue", "rebeccapurple"

The alpha channel of rgba is how you make translucent fills, for example a confidence band. Plotly validates color strings, so a typo such as "crimsom" raises an error immediately.

Discrete colors: sequences and maps

Express colors categories from a sequence, a list of colors used in order. The default is px.colors.qualitative.Plotly. Many alternatives live in the same module: D3, G10, T10, Set1, Set2, Set3, Pastel, Dark2, Safe, Vivid, Bold, Antique and more.

Python Qualitative palettes and explicit color maps

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

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

color_discrete_map is the right tool when a category must always have the same color across charts ("Product A is always blue") or when you want to highlight one category and gray out the rest, a classic way to focus attention.

When you build traces by hand, you set marker_color or line_color on each trace. If you do not, plotly.js picks colors from the layout's colorway.

Continuous scales

A color scale maps numbers to colors. In Express, set color to a numeric column and choose the scale with color_continuous_scale. The scales come in families, and the family matters:

Family Use for Examples
Sequential Amounts from low to high Viridis, Plasma, Blues, Greens, YlOrRd, Cividis
Diverging Values around a meaningful center (zero, average) RdBu, PiYG, Tealrose, Spectral, Picnic
Cyclical Angles, time of day, months Twilight, IceFire, HSV

Add _r to a name to reverse it: "Blues_r" goes from dark to light. Browse them with px.colors.sequential, px.colors.diverging and px.colors.cyclical.

Python Continuous scales, ranges and midpoints

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

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Two arguments deserve special attention. range_color=[low, high] pins the scale to fixed limits so the same value always has the same color across charts. color_continuous_midpoint centers a diverging scale on a value. Without it, white in the middle would fall on the midpoint of the data range, not on the number that matters, such as zero.

Writing your own color scale

A color scale is a list of [position, color] pairs. The positions run from 0 to 1 and are stretched across the data range (or across zmin to zmax).

A custom color scale and a styled colorbar

The white stop at position 0.5 lines up with z = 0 because zmin and zmax are symmetric. The same list goes to color_continuous_scale in Express or colorscale in graph objects.

The color bar

The bar next to continuous charts has settings of its own:

  • title, thickness, len (length as a fraction), x, y (position) and orientation="h" for a horizontal bar.
  • tickvals, ticktext, ticksuffix and tickformat for ticks.
  • In Express charts, use fig.update_coloraxes(colorbar_title="..."); set coloraxis_showscale=False to hide the bar.
Python Style the coloraxis in Python

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

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

zmid is the graph-objects equivalent of Express' color_continuous_midpoint.

Making color work for everyone

About 1 in 12 men and 1 in 200 women have some form of color-vision deficiency. A few habits help all readers:

  • Prefer perceptually uniform scales (Viridis, Cividis, Plasma) over rainbow scales such as Jet, which create false boundaries.
  • Do not rely on color alone for categories: add symbol, line_dash or pattern_shape, or label the data directly.
  • Use the Safe or T10 palettes, or the Okabe-Ito colors (#E69F00, #56B4E9, #009E73, #F0E442, #0072B2, #D55E00, #CC79A7).
  • Keep enough contrast with the background; light yellow on white disappears.
  • Use gray for context and one strong color for what matters.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which argument gives a specific color to each category in Express?

    color_discrete_map maps category values to colors. colorway is a layout-level default sequence.
  2. 2. What type of scale should you use for correlations between -1 and 1?

    Values around a meaningful center need a diverging scale with a fixed midpoint.
  3. 3. How do you reverse the color scale Blues?

    Adding _r to a scale name reverses it.
  4. 4. In a hand-made colorscale, what do the numbers in [0.5, "white"] mean?

    Positions run from 0 (the minimum) to 1 (the maximum) and are stretched over the data range.
  5. 5. Why use range_color (or zmin and zmax)?

    Without fixed limits, the scale adapts to each chart's data.

Summary

  • Colors accept hex, rgb/rgba, hsl and CSS names; Plotly validates them.
  • Discrete colors come from sequences (color_discrete_sequence), explicit maps (color_discrete_map) or the layout colorway.
  • Continuous colors use sequential, diverging or cyclical scales; control them with range_color, color_continuous_midpoint, _r and custom [position, color] lists.
  • Style the color bar through colorbar or update_coloraxes, and design for color-vision differences.

Next: Templates and Themes