plot_get_clist

plot_get_clist(n_colors=3, kind='categorical', cmap=None, facecolor_dark=False)[source]

Get a list of n_colors colors for a categorical, continuous, or diverging palette.

This is the single entry point for quickly obtaining a color list of any size and type. Following Matplotlib’s colormap taxonomy, kind selects the palette family and the optional cmap selects the concrete palette within it:

  • categorical (qualitative): maximally distinct colors for discrete classes. Hand-curated for 2 to 9 colors, the 'husl' palette for 10 to 20.

  • continuous (sequential or qualitative): n_colors sampled from any named palette. An ordered map such as 'viridis' yields a perceptual ramp (encodes magnitude); a qualitative map such as 'husl' yields distinct hues.

  • diverging: two hues from a neutral center, for signed/centered data. The house 'CPP' / 'SHAP' maps or any Matplotlib diverging map (e.g. 'coolwarm').

For categorical / continuous the colors are produced via seaborn.color_palette(), so any Matplotlib colormap name plus seaborn’s 'husl' / 'hls' generators are accepted as cmap; see the seaborn palette documentation for the full set. For a full pre-sized diverging colormap (101 points by default) use plot_get_cmap() instead.

Added in version 0.1.2.

Parameters:
  • n_colors (int, default=3) – Number of colors. Must be at least 2 (categorical, at most 20) or 3 (continuous / diverging).

  • kind ({'categorical', 'continuous', 'diverging'}, default='categorical') – Palette family to draw colors from.

  • cmap (str, optional) – Name of the concrete palette. If None, defaults to the curated list (categorical), 'husl' (continuous), or 'CPP' (diverging). Accepts any Matplotlib colormap name and seaborn’s 'husl' / 'hls'; 'CPP' and 'SHAP' are valid only for kind='diverging'.

  • facecolor_dark (bool, default=False) – Whether the central color of a diverging map is black (if True) or white (if False). Only applies to kind='diverging'; ignored otherwise.

Returns:

colors – List of n_colors colors. Matplotlib color-name strings for the curated categorical list, otherwise RGB tuples.

Return type:

list

See also

Examples

You can retrieve a list of n colors by using the n_colors parameter:

import matplotlib.pyplot as plt
import seaborn as sns
import aaanalysis as aa
colors = aa.plot_get_clist(n_colors=2)
sns.palplot(colors)
plt.show()
../_images/plot_get_clist_1_output_1_0.png

We assembled 8 different color lists for 2 to 9 colors:

for n in range(3, 9):
    colors = aa.plot_get_clist(n_colors=n)
    sns.palplot(colors)
    plt.show()
../_images/plot_get_clist_2_output_3_0.png ../_images/plot_get_clist_3_output_3_1.png ../_images/plot_get_clist_4_output_3_2.png ../_images/plot_get_clist_5_output_3_3.png ../_images/plot_get_clist_6_output_3_4.png ../_images/plot_get_clist_7_output_3_5.png

For more than 9 colors, we provide the ‘husl’ default color palette of the :func:seaborn.color_palette function:

for n in [10, 15, 20]:
    colors = aa.plot_get_clist(n_colors=n)
    sns.palplot(colors)
    plt.show()
../_images/plot_get_clist_8_output_5_0.png ../_images/plot_get_clist_9_output_5_1.png ../_images/plot_get_clist_10_output_5_2.png

Use the kind parameter to switch the palette family (following Matplotlib’s colormap taxonomy) and cmap to pick a concrete palette within it. For categorical and continuous palettes the colors come from :func:seaborn.color_palette, so any Matplotlib colormap name plus ‘husl’/‘hls’ is accepted; see the seaborn palette documentation for the full set.

For kind='categorical' you can pass any qualitative cmap (e.g. ‘Set2’, ‘tab20’, ‘hls’) instead of the curated default:

for cmap in ["Set2", "tab20", "hls"]:
    colors = aa.plot_get_clist(n_colors=8, kind="categorical", cmap=cmap)
    sns.palplot(colors)
    plt.show()
../_images/plot_get_clist_11_output_8_0.png ../_images/plot_get_clist_12_output_8_1.png ../_images/plot_get_clist_13_output_8_2.png

For kind='continuous', an ordered colormap such as ‘viridis’ gives a perceptual ramp (encoding magnitude), while a qualitative one such as ‘husl’ (the default) gives distinct hues:

for cmap in ["viridis", "magma", "husl"]:
    colors = aa.plot_get_clist(n_colors=12, kind="continuous", cmap=cmap)
    sns.palplot(colors)
    plt.show()
../_images/plot_get_clist_14_output_10_0.png ../_images/plot_get_clist_15_output_10_1.png ../_images/plot_get_clist_16_output_10_2.png

For kind='diverging', use the house ‘CPP’/‘SHAP’ maps or any Matplotlib diverging map (e.g. ‘coolwarm’):

for cmap in ["CPP", "SHAP", "coolwarm"]:
    colors = aa.plot_get_clist(n_colors=11, kind="diverging", cmap=cmap)
    sns.palplot(colors)
    plt.show()
../_images/plot_get_clist_17_output_12_0.png ../_images/plot_get_clist_18_output_12_1.png ../_images/plot_get_clist_19_output_12_2.png

The facecolor_dark parameter switches the central color of a diverging map between white (default) and black:

colors = aa.plot_get_clist(n_colors=11, kind="diverging", cmap="CPP", facecolor_dark=True)
sns.palplot(colors)
plt.show()
../_images/plot_get_clist_20_output_14_0.png