publiplots.color_palette¶
- publiplots.color_palette(palette=None, n_colors=None, desat=None, as_cmap=False, divergent=None)[source]¶
Return a color palette as a list of hex colors or a colormap.
Integrates with seaborn — custom publiplots palettes (see
PALETTES) take precedence over seaborn palette names.- Parameters:
palette (str or list, optional) – Palette name or explicit list of colors. Name resolution checks publiplots palettes first, then seaborn. If
None, usespp.rcParams["palette"](default"pastel"). Append"_r"to reverse any named palette (e.g."coolwarm_r","viridis_r").n_colors (int, optional) – Number of colors to return. If
None, returns the full palette.desat (float, optional) – Proportion to desaturate each color, in
[0, 1].as_cmap (bool, default
False) – IfTrue, return amatplotlib.colors.ListedColormapinstead of a list.divergent (bool, optional) – If
True, sample colors to preserve extremes (first / last / evenly spaced) — appropriate for diverging palettes. IfNone, auto-detected for palettes listed inDIVERGENT_PALETTES. SetFalseto disable sampling even for known divergent palettes.
- Returns:
List of hex color codes, or a colormap when
as_cmap=True.- Return type:
Examples
Get custom pastel palette:
>>> colors = color_palette("pastel") >>> len(colors) 12
Get seaborn palette:
>>> colors = color_palette("viridis", n_colors=5)Get as colormap:
>>> cmap = color_palette("pastel", as_cmap=True)Divergent palette sampled at 3 colors (first, middle, last):
>>> colors = color_palette("RdBu", n_colors=3)Force divergent sampling on any palette:
>>> colors = color_palette("viridis", n_colors=5, divergent=True)Reverse any palette with the
"_r"suffix (seaborn convention):>>> colors = color_palette("RdBu_r", n_colors=3) >>> colors = color_palette("viridis_r", n_colors=5)
See also
PALETTESDictionary of built-in publiplots palettes.
DIVERGENT_PALETTESNames auto-detected as divergent.