Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Seaborn · Function

color_palette

Build a Seaborn palette from a named palette, colormap, palette specification, or explicit colors. It can return discrete colors or a continuous colormap.

How it works

color_palette() accepts a named Seaborn or Matplotlib palette, a supported palette family specification, or a sequence of Matplotlib-compatible colors. It returns a color-palette object by default; setting as_cmap=True instead produces a continuous colormap. The number of colors and saturation can be adjusted with n_colors and desat.

Syntax

seaborn.color_palette(palette=None, n_colors=None, desat=None, as_cmap=False)

Parameters

NameTypeDescription
palettestring, sequence, or NonePalette definition: a Seaborn palette name, Matplotlib colormap name, supported palette specification, or sequence of Matplotlib-compatible colors.
n_colorsint or NoneNumber of colors in the palette. If omitted, the default depends on the palette specification.
desatfloat or NoneProportion by which to desaturate each color.
as_cmapboolIf true, return a continuous colormap instead of a discrete palette.

Returns

A list-like color palette, or a continuous colormap when as_cmap=True.

Key points

  • Use the function for either a discrete color collection or a continuous colormap.
  • Several built-in qualitative Seaborn palettes can be selected directly by name.
  • Matplotlib colormap names are valid palette specifications.
  • The HUSL and HLS hue systems are available through the palette argument.
  • You can provide explicit colors using formats supported by Matplotlib.
  • The discrete result wraps RGB tuples and provides palette-oriented utilities.
  • The returned palette supports hexadecimal conversion and rich display in HTML environments.

Common mistakes

  • n_colors has no effect when requesting a continuous colormap with as_cmap=True.
  • Requesting more colors than a finite palette provides repeats its colors.

Example: Create a discrete palette and a colormap

import seaborn as sns

palette = sns.color_palette("deep", n_colors=4)
print(palette.as_hex())

cmap = sns.color_palette("Spectral", as_cmap=True)
print(cmap(0.5))

Creates four colors from Seaborn’s named categorical palette and converts them to hexadecimal strings, then creates a continuous colormap from a ColorBrewer palette.

When to use it

Choose a named qualitative palette

Need consistent categorical colors for a plot.

Create a continuous colormap

Need a scalar-to-color mapping for a heatmap or continuous plot.

Supply explicit Matplotlib-compatible colors

Need exact brand or domain colors.

Adjust saturation with desat

Need a lighter or less saturated visual treatment.

Interview questions

QWhat kinds of values can palette accept?

The palette argument can be a Seaborn palette name, a Matplotlib colormap name, husl or hls, supported palette specifications such as ch:<cubehelix arguments>, or a sequence of Matplotlib-compatible colors. With palette=None, the function uses the current Matplotlib color cycle.

Follow-up: How would you obtain a continuous mapping rather than discrete colors? Pass as_cmap=True; the return value is then a continuous colormap.

QHow does n_colors behave?

n_colors controls the requested palette length when a discrete palette is returned. If it is omitted, the default depends on how palette is specified; named palettes generally default to six colors.

Follow-up: What happens if the requested length exceeds the palette size? The palette cycles through its available colors.

QWhat does color_palette() return?

The default return is a list-like Seaborn palette object containing RGB tuples and methods such as hexadecimal conversion. With as_cmap=True, the function returns a continuous colormap instead.

Follow-up: How can the discrete result be made easier to serialize for web use? Call its as_hex() method to obtain hexadecimal color strings.

  • set_palette
  • husl_palette
  • hls_palette
  • cubehelix_palette
  • dark_palette
  • light_palette
  • diverging_palette
  • blend_palette

Source: seaborn.color_palette. Checked 2026-10-15.