Skip to content
Tutorials Center
My learning

Topics

Lesson 31 of 38 ยท Styling and Themes

Colour Palettes and Colormaps in Seaborn

Choose and build seaborn colour palettes: qualitative, sequential and diverging, color_palette, cubehelix, light_palette, custom colours and colour-blind safe options.

Colour in a chart is data, not decoration. A good palette makes groups distinguishable, shows order in numeric values and stays readable for colour-blind viewers. A bad one invents patterns or hides real ones. Seaborn ships many ready-made palettes and a toolkit for building your own.

This lesson organises palettes into three families by the kind of data they suit, shows how to create and use them, and ends with accessibility advice.

Three families

Match the palette to the data

Qualitative Unordered categories. Distinct hues.
Sequential Low to high. One hue, light to dark.
Diverging Above and below a midpoint. Two hues.

Never use a qualitative palette for ordered numbers.

1 / 2

Looking at a palette

sns.color_palette(name, n) returns a list of RGB colours, and sns.palplot() is deprecated, so use a small plot to display it (the object also renders in notebooks as a strip). Here is every family at a glance:

Python Preview palettes from each family

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.

The first three rows are qualitative, the next three are sequential, the last three are diverging. Names ending in _r reverse a palette ("rocket_r"), and a trailing number in color_palette("husl", 12) asks for that many colours.

Qualitative palettes

Seaborn's default is "deep", with "muted", "pastel", "bright", "dark" and "colorblind" as variants of the same hues. Matplotlib's "Set1", "Set2", "Set3", "tab10" and "Paired" work too. For more than eight categories use "husl" or "hls", which space hues evenly around the colour wheel.

Apply one with palette=:

Python Applying qualitative palettes

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.

Since seaborn 0.13 a palette needs a hue to take effect; for a single-colour category axis, set hue="team" and legend=False as above (or use color= for one colour).

You can also pass a list or dictionary. A dictionary pins each category to a colour consistently across charts, which matters when readers compare figures:

Python
palette = {"Ant": "#4c78a8", "Bee": "#f58518", "Cat": "#54a24b", "Dog": "#e45756"}
sns.boxplot(data=df, x="team", y="score", hue="team", palette=palette, legend=False)

Sequential palettes

For numbers that go from low to high, use a palette that changes in lightness: "rocket", "mako", "flare", "crest", "viridis", "magma" (perceptually uniform), or single-hue maps like "Blues". Because the colours are ordered, hue on a numeric column picks a continuous map:

Python Sequential colour for a numeric hue

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.

Diverging palettes

When values spread around a meaningful midpoint (profit and loss, above and below average) use two hues that meet at a light neutral. Built-ins: "vlag", "icefire", "coolwarm", "RdBu", "Spectral". Build your own with sns.diverging_palette(h_neg, h_pos, s=75, l=50, n=7, center="light"), where the hues are angles on the colour wheel (240 is blue, 10 is red, 145 is green).

Building custom palettes

Function Makes
sns.light_palette(color, n) Light-to-colour sequential palette from one colour
sns.dark_palette(color, n) Dark-to-colour sequential palette
sns.cubehelix_palette(n, start, rot) Smooth sequential palette that also prints well in greyscale
sns.diverging_palette(h1, h2) Diverging palette from two hues
sns.blend_palette([c1, c2, c3]) Blend between any colours you choose
sns.husl_palette(n) n evenly spaced hues of equal brightness

Pass as_cmap=True to get a continuous colormap for heatmap(cmap=...) instead of a list.

Python Custom palettes in a heatmap

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.

Setting the default and temporary palettes

sns.set_palette("Set2") (or set_theme(palette=...)) changes the default colour cycle. Use with sns.color_palette("pastel"): for one figure. sns.color_palette() returns the active palette, useful for matching colours in annotations.

Accessibility

About 1 in 12 men and 1 in 200 women have some form of colour-vision deficiency, mostly red-green. To stay safe:

  • Use "colorblind" for categories, or viridis/rocket/mako for sequential data.
  • Do not rely on red versus green alone. Add markers (style=), line styles or direct labels.
  • Keep sequential palettes monotonic in lightness; check by converting the figure to greyscale.
  • Limit categories to about 6-8 colours. Beyond that, facet the chart (FacetGrid).

More on colour mapping in Matplotlib is in colormaps.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which palette family suits unordered categories?

    Qualitative palettes use distinct hues with no implied order.
  2. 2. Which palette suits profit and loss around zero?

    A diverging palette has two hues meeting at a neutral midpoint.
  3. 3. How do you obtain a continuous colormap object from light_palette?

    as_cmap=True returns a Matplotlib colormap suitable for heatmap(cmap=...).
  4. 4. How do you reverse a palette?

    Appending _r, as in rocket_r, reverses the palette.

Summary

  • Pick the family by data type: qualitative for categories, sequential for low-to-high, diverging around a midpoint.
  • palette= takes a name, list or dictionary; seaborn 0.13 needs hue for it to apply.
  • Build your own with light_palette, dark_palette, cubehelix_palette, diverging_palette, blend_palette; add as_cmap=True for heatmaps.
  • Choose colour-blind safe palettes and do not rely on colour alone.

Next: Error Bars and Confidence Intervals