Skip to content

Fix color_palette(as_cmap=True) returning a list instead of a colormap for several palette specs - #4032

Open
DresdenGman wants to merge 4 commits into
mwaskom:masterfrom
DresdenGman:fix-3448-color-palette-as-cmap
Open

DresdenGman wants to merge 4 commits into
mwaskom:masterfrom
DresdenGman:fix-3448-color-palette-as-cmap

Conversation

@DresdenGman

@DresdenGman DresdenGman commented Nov 4, 2026 •

Copy link
Copy Markdown

Summary

Fixes #3448: sns.color_palette(as_cmap=True) returned a plain list of colors instead of a colormap for several palette specifications, despite the documented contract ("If True, return a matplotlib.colors.ListedColormap").

Before this change, as_cmap=True only produced a colormap when the palette generator itself built one (e.g. "hls", "ch:", "light:...", or a matplotlib colormap name). It returned a list for:

  • the default palette / current color cycle — the exact call from the issue, sns.color_palette(as_cmap=True)
  • named seaborn palettes ("deep", "muted", "deep6", ...)
  • a sequence of colors passed directly
  • any of the above combined with desat

Two adjacent cases raised TypeError instead:

  • color_palette("hls", desat=0.5, as_cmap=True) — desaturation tried to iterate the colormap already produced by the generator ('ListedColormap' object is not iterable)
  • passing a colormap object, color_palette(some_cmap, as_cmap=True) — failed at len(palette) (object of type 'ListedColormap' has no len())

Changes (seaborn/palettes.py, color_palette only)

  • When as_cmap=True and the resolved palette is still a list of colors, wrap it in mpl.colors.ListedColormap before returning (matching the conversion in seaborn/_base.py and seaborn/distributions.py). Palettes that already resolve to a colormap are returned unchanged.
  • When desat is combined with a palette whose generator already returned a colormap, recover its discrete colors first (the stored colors for a ListedColormap; evenly spaced samples for other colormaps), desaturate them, and let the step above wrap them back into a ListedColormap.
  • The returned colormap copies the color list instead of aliasing it, so mutating the colormap cannot corrupt the caller's list or the module-level palette registry. The as_cmap docstring now documents the colormap-input and empty-palette contract.
  • Skip len(palette) for colormap inputs so a colormap passed with as_cmap=True is returned as-is (n_colors is documented as ignored when as_cmap=True).

Behavior contract for the edge cases

  • Desaturating a colormap changes saturation only. The returned ListedColormap preserves each color's alpha channel, the source colormap's name, and its bad color, and it carries over the under and over colors when they were explicitly set on the source (when they were not set, they resolve from the desaturated ends of the new colors, as they would on the source). The source colormap is not mutated.
  • Passing a colormap object is supported with as_cmap=True: it is returned unchanged, or as the desaturated copy described above when desat is given; n_colors is ignored, as documented. With as_cmap=False, it raises TypeError with a message explaining that a list of colors cannot be returned from a colormap object. (Previously, the as_cmap=False combination also raised TypeError, but with incidental messages from inside the function.)
  • Empty palettes: requesting a colormap for an empty palette, color_palette([], as_cmap=True), raises ValueError rather than returning a zero-length colormap (which would map every value to the bad color). color_palette([], as_cmap=False) still returns an empty palette, as before.
  • Colormap pass-through vs. the docstring: when a palette spec already resolves to a colormap — such as "viridis" or a colormap object passed directly — that colormap is returned unchanged, so the result is not always a ListedColormap (e.g. the registry's LinearSegmentedColormap for "viridis"). This pass-through is pre-existing behavior (mpl_palette returns the registry colormap, and test_mpl_palette relies on it) and is unchanged by this PR; only palettes that resolve to a list of colors are newly wrapped in a ListedColormap.
  • as_cmap=False behavior for all other inputs is unchanged, and n_colors remains ignored when as_cmap=True (a named palette's colormap contains all of its colors).

Tests

Eleven new colormap tests in tests/test_palettes.py covering: named seaborn palettes, the current color cycle, a color sequence, colormap pass-through, desat combined with as_cmap=True (with numeric assertions on the desaturated colors, including the "hls" case that previously raised TypeError, plus "husl" and a registered continuous colormap), metadata preservation when desaturating listed and continuous colormaps (name, alpha, bad/under/over), source-colormap non-mutation, TypeError for a colormap object with as_cmap=False, the ValueError for an empty palette, n_colors being ignored with as_cmap=True, and the returned colormap not aliasing the source list. All eleven fail on the unfixed code and pass with this change.

Local results (Python 3.12.3, matplotlib 3.8.4, numpy 1.26.4, pandas 2.2.2):

  • tests/test_palettes.py: 49 passed (38 existing + 11 new)
  • Baseline comparison over tests/test_palettes.py, tests/test_rcmod.py, tests/test_utils.py, tests/_core/test_properties.py: 277 passed / 0 failed with this change vs 266 passed / 0 failed on unmodified master, with the delta being exactly the 11 new tests

- Desaturating a colormap now preserves each color's alpha channel
  and restores the source colormap's name and bad/under/over colors
  on the returned ListedColormap
- Passing a colormap object with as_cmap=False now raises a clear
  TypeError in all desat/n_colors combinations instead of failing
  incidentally (or silently succeeding for one combination)
- Requesting a colormap for an empty palette raises ValueError
  instead of returning a zero-length ListedColormap
- Tests: numeric assertions for hls+desat, metadata preservation for
  listed and continuous colormaps, the as_cmap=False error matrix,
  the empty-palette error, and n_colors being ignored with as_cmap
- Assert unset under/over stay unset after desaturating a continuous
  colormap (guards the conditional set_under/set_over restore)
- Assert the source colormap's colors are unchanged by the call
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sns.color_palette(as_cmap= True) returns no cmap

1 participant