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, uses pp.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) – If True, return a matplotlib.colors.ListedColormap instead of a list.

  • divergent (bool, optional) – If True, sample colors to preserve extremes (first / last / evenly spaced) — appropriate for diverging palettes. If None, auto-detected for palettes listed in DIVERGENT_PALETTES. Set False to disable sampling even for known divergent palettes.

Returns:

List of hex color codes, or a colormap when as_cmap=True.

Return type:

list of str or matplotlib.colors.ListedColormap

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

PALETTES

Dictionary of built-in publiplots palettes.

DIVERGENT_PALETTES

Names auto-detected as divergent.