Choose a named qualitative palette
Need consistent categorical colors for a plot.
Seaborn · Function
Build a Seaborn palette from a named palette, colormap, palette specification, or explicit colors. It can return discrete colors or a continuous colormap.
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.
seaborn.color_palette(palette=None, n_colors=None, desat=None, as_cmap=False)| Name | Type | Description |
|---|---|---|
palette | string, sequence, or None | Palette definition: a Seaborn palette name, Matplotlib colormap name, supported palette specification, or sequence of Matplotlib-compatible colors. |
n_colors | int or None | Number of colors in the palette. If omitted, the default depends on the palette specification. |
desat | float or None | Proportion by which to desaturate each color. |
as_cmap | bool | If true, return a continuous colormap instead of a discrete palette. |
A list-like color palette, or a continuous colormap when as_cmap=True.
n_colors has no effect when requesting a continuous colormap with as_cmap=True.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.
Need consistent categorical colors for a plot.
Need a scalar-to-color mapping for a heatmap or continuous plot.
Need exact brand or domain colors.
Need a lighter or less saturated visual treatment.
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.
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.
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_palettehusl_palettehls_palettecubehelix_palettedark_palettelight_palettediverging_paletteblend_palette