alphapepttools.pl.get_color_mapping

alphapepttools.pl.get_color_mapping#

alphapepttools.pl.get_color_mapping(values, palette)#

Map categorical values to colors

Maps unique values in values to colors from palette. If palette is a list of colors, colors are assigned in a cycling manner, which can lead to non-unique assignments if there are more unique values than colors in the palette. If palette is a colormap, colors are assigned uniquely based on the number of unique values. Missing values (as defined in config["na_identifiers"]) are assigned the default NA color (config["na_color"]).

Parameters:
  • values (np.ndarray) – Values to map to colors

  • palette (list[str | tuple] | mpl.colors.Colormap) – If palette is a list, it is used as a discrete colormap with cycling colors (potential for non-unique assignment). If palette is a colormap, it is used as a continuous colormap (guaranteed unique assignment).

Return type:

dict

Returns:

dict Dictionary mapping values to colors

Examples

Map categorical levels to a color palette:

import numpy as np

levels = np.array(["A", "B", "C", "A", "B"])
palette = ["red", "green", "blue"]
color_dict = get_color_mapping(levels, palette)

Map using a matplotlib colormap:

import matplotlib.pyplot as plt

levels = np.array(["low", "medium", "high"])
cmap = plt.get_cmap("viridis")
color_dict = get_color_mapping(levels, cmap)