Skip to content

PanelColorManager

yohou.plotting.PanelColorManager

Assign consistent colours to panel members by name.

Unlike positional indexing (where the first column always gets colour 0), this class maps member names to colours. The same name always receives the same colour within a manager instance, regardless of the order it appears in different panel groups.

Parameters

Name Type Description Default
color_palette list[str] | None

Custom colour hex codes. Any falsy value (None or an empty list []) falls back to the default palette_yohou palette. Note this differs from resolve_color_palette, which raises ValueError for an empty list.

None

Examples

>>> mgr = PanelColorManager()
>>> mgr.get_color("a")
'#2563EB'
>>> mgr.get_color("b")
'#DC2626'
>>> mgr.get_color("a")  # same name -> same colour
'#2563EB'

Source Code

Source code in src/yohou/plotting/_utils.py
class PanelColorManager:
    """Assign consistent colours to panel members by name.

    Unlike positional indexing (where the first column always gets
    colour 0), this class maps *member names* to colours.  The same
    name always receives the same colour within a manager instance,
    regardless of the order it appears in different panel groups.

    Parameters
    ----------
    color_palette : list[str] | None, default=None
        Custom colour hex codes.  Any falsy value (``None`` or an empty
        list ``[]``) falls back to the default `palette_yohou` palette.
        Note this differs from `resolve_color_palette`, which raises
        ``ValueError`` for an empty list.

    Examples
    --------
    >>> mgr = PanelColorManager()
    >>> mgr.get_color("a")
    '#2563EB'
    >>> mgr.get_color("b")
    '#DC2626'
    >>> mgr.get_color("a")  # same name -> same colour
    '#2563EB'
    """

    def __init__(self, color_palette: list[str] | None = None) -> None:
        self._palette = color_palette or list(palette_yohou().values())
        self._name_to_color: dict[str, str] = {}
        self._counter = 0

    def get_color(self, member_name: str) -> str:
        """Return the colour assigned to *member_name*."""
        if member_name not in self._name_to_color:
            self._name_to_color[member_name] = self._palette[self._counter % len(self._palette)]
            self._counter += 1
        return self._name_to_color[member_name]

    def get_colors(self, names: list[str]) -> list[str]:
        """Return colours for a list of member names."""
        return [self.get_color(n) for n in names]

Methods

get_color(member_name)

Return the colour assigned to member_name.

Source Code
Source code in src/yohou/plotting/_utils.py
def get_color(self, member_name: str) -> str:
    """Return the colour assigned to *member_name*."""
    if member_name not in self._name_to_color:
        self._name_to_color[member_name] = self._palette[self._counter % len(self._palette)]
        self._counter += 1
    return self._name_to_color[member_name]

get_colors(names)

Return colours for a list of member names.

Source Code
Source code in src/yohou/plotting/_utils.py
def get_colors(self, names: list[str]) -> list[str]:
    """Return colours for a list of member names."""
    return [self.get_color(n) for n in names]