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 colormap.

Integrates with seaborn - custom publiplots palettes override seaborn defaults.

Parameters:
  • palette (str, list, or None) – Palette name (checks publiplots first, then seaborn) or list of colors. If None, uses the default palette from rcParams. Append “_r” suffix to reverse any palette (e.g., “coolwarm_r”, “viridis_r”).

  • n_colors (int, optional) – Number of colors in the palette.

  • desat (float, optional) – Proportion to desaturate each color.

  • as_cmap (bool, default=False) – If True, return a matplotlib colormap instead of a list.

  • divergent (bool, optional) – If True, sample colors to preserve extremes (for divergent palettes). If None (default), automatically detected for known divergent palettes. Set to False to disable divergent sampling even for known divergent palettes.

Returns:

List of hex color codes or colormap if as_cmap=True.

Return type:

list 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)

Get divergent palette with 3 colors (first, middle, last): >>> colors = color_palette(“coolwarm”, n_colors=3)

Force divergent sampling on any palette: >>> colors = color_palette(“viridis”, n_colors=5, divergent=True)

Reverse any palette with “_r” suffix (seaborn convention): >>> colors = color_palette(“coolwarm_r”, n_colors=3) >>> colors = color_palette(“viridis_r”, n_colors=5)