From 0678ac73302ece7fccdcc3560a1083b92eefdc0c Mon Sep 17 00:00:00 2001 From: Olivier Burggraaff Date: Tue, 28 Jul 2026 11:26:46 +0100 Subject: [PATCH 1/2] Handle ListedColormaps in expand and cmap_and_norm --- src/earthkit/plots/styles/colors/__init__.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/earthkit/plots/styles/colors/__init__.py b/src/earthkit/plots/styles/colors/__init__.py index 6fb83817..aa2361dc 100644 --- a/src/earthkit/plots/styles/colors/__init__.py +++ b/src/earthkit/plots/styles/colors/__init__.py @@ -69,6 +69,8 @@ def expand(colors, levels, extend_colors=0): colors = [colors] * (length - 1) else: colors = [cmap(i) for i in np.linspace(0, 1, length)] + elif isinstance(colors, ListedColormap): + colors = list(colors.colors) elif isinstance(colors, mpl.colors.Colormap): colors = [colors(i) for i in np.linspace(0, 1, length)] return colors @@ -108,6 +110,7 @@ def cmap_and_norm(colors, levels, normalize=True, extend=None, extend_levels=Tru Whether to extend the levels. If False, the levels will be used as is. If True, the levels will be extended to include the under and over values. """ + is_listed = isinstance(colors, ListedColormap) levels = list(levels) extend_colors = 0 color_levels = levels @@ -134,7 +137,6 @@ def cmap_and_norm(colors, levels, normalize=True, extend=None, extend_levels=Tru N = len(color_levels) + extend_colors - 1 colormap = LinearSegmentedColormap.from_list - is_listed = colors is not None and len(colors) == N if is_listed: colormap = ListedColormap From a2500af6099ba4a78a9e7f56189560ff8d3b7cc4 Mon Sep 17 00:00:00 2001 From: Olivier Burggraaff Date: Tue, 28 Jul 2026 11:39:29 +0100 Subject: [PATCH 2/2] Copy definition of colors from expand to cmap_and_norm --- src/earthkit/plots/styles/colors/__init__.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/earthkit/plots/styles/colors/__init__.py b/src/earthkit/plots/styles/colors/__init__.py index aa2361dc..9d0b8dde 100644 --- a/src/earthkit/plots/styles/colors/__init__.py +++ b/src/earthkit/plots/styles/colors/__init__.py @@ -98,8 +98,9 @@ def cmap_and_norm(colors, levels, normalize=True, extend=None, extend_levels=Tru Parameters ---------- - colors : str or list - The name of a matplotlib colormap or a list of colours. + colors : str or list or matplotlib.colors.Colormap + The name of a matplotlib colormap, a list of colours, or a matplotlib + colormap object (e.g. a ``ListedColormap``). levels : list The levels for which to generate colours. normalize : bool, optional