pycea.get.palette

Contents

pycea.get.palette#

pycea.get.palette(tdata, key, custom=None, cmap=None, priors=None, sort=None, random_state=None)#

Get color palette for a given key.

This function gets the mapping from category → color for a given key in tdata. If no customizations are provided, the function will return a previously stored palette in tdata.uns if it exists. Otherwise, a new palette is generated.

Parameters:
  • tdata (TreeData) – The treedata.TreeData object.

  • key (str) – A key from obs.keys(), obsm.keys(), or obsp.keys() to generate a color palette for.

  • custom (Mapping[Any, Any] | None (default: None)) – A dictionary mapping specific values to colors (e.g., {"category1": "red"}).

  • cmap (str | Colormap | Cycler | None (default: None)) – A colormap to use for generating colors. If None, mpl.rcParams["axes.prop_cycle"] is used.

  • priors (Mapping[Any, float] | None (default: None)) – A dictionary mapping values to their prior probabilities. Values with higher priors will be assigned less saturated colors.

  • sort (str | None (default: None)) – How to sort the categories. One of None, "alphabetical", "frequency", or "random". If None, existing categories order is used or natural sorting.

  • random_state (int | None (default: None)) – Random seed for sampling. Only used if sort is "random".

Return type:

dict[Any, Any]

Returns:

palette - Color palette for the given key

Examples

Get character color palette with saturation adjusted by indel probability:

>>> tdata = py.datasets.yang22()
>>> indel_palette = py.get.palette(
...     tdata,
...     "characters",
...     custom={"-": "white", "*": "lightgrey"},
...     cmap="gist_rainbow",
...     priors=tdata.uns["priors"],
...     sort="random",
... )