Repository navigation
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
Open
Fix color_palette(as_cmap=True) returning a list instead of a colormap for several palette specs#4032DresdenGman wants to merge 4 commits into
DresdenGman wants to merge 4 commits into
Conversation
- 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
…t, extend desat tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 amatplotlib.colors.ListedColormap").Before this change,
as_cmap=Trueonly 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:sns.color_palette(as_cmap=True)"deep","muted","deep6", ...)desatTwo adjacent cases raised
TypeErrorinstead: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)color_palette(some_cmap, as_cmap=True)— failed atlen(palette)(object of type 'ListedColormap' has no len())Changes (
seaborn/palettes.py,color_paletteonly)as_cmap=Trueand the resolved palette is still a list of colors, wrap it inmpl.colors.ListedColormapbefore returning (matching the conversion inseaborn/_base.pyandseaborn/distributions.py). Palettes that already resolve to a colormap are returned unchanged.desatis combined with a palette whose generator already returned a colormap, recover its discrete colors first (the stored colors for aListedColormap; evenly spaced samples for other colormaps), desaturate them, and let the step above wrap them back into aListedColormap.as_cmapdocstring now documents the colormap-input and empty-palette contract.len(palette)for colormap inputs so a colormap passed withas_cmap=Trueis returned as-is (n_colorsis documented as ignored whenas_cmap=True).Behavior contract for the edge cases
ListedColormappreserves 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.as_cmap=True: it is returned unchanged, or as the desaturated copy described above whendesatis given;n_colorsis ignored, as documented. Withas_cmap=False, it raisesTypeErrorwith a message explaining that a list of colors cannot be returned from a colormap object. (Previously, theas_cmap=Falsecombination also raisedTypeError, but with incidental messages from inside the function.)color_palette([], as_cmap=True), raisesValueErrorrather 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."viridis"or a colormap object passed directly — that colormap is returned unchanged, so the result is not always aListedColormap(e.g. the registry'sLinearSegmentedColormapfor"viridis"). This pass-through is pre-existing behavior (mpl_palettereturns the registry colormap, andtest_mpl_paletterelies on it) and is unchanged by this PR; only palettes that resolve to a list of colors are newly wrapped in aListedColormap.as_cmap=Falsebehavior for all other inputs is unchanged, andn_colorsremains ignored whenas_cmap=True(a named palette's colormap contains all of its colors).Tests
Eleven new colormap tests in
tests/test_palettes.pycovering: named seaborn palettes, the current color cycle, a color sequence, colormap pass-through,desatcombined withas_cmap=True(with numeric assertions on the desaturated colors, including the"hls"case that previously raisedTypeError, plus"husl"and a registered continuous colormap), metadata preservation when desaturating listed and continuous colormaps (name, alpha, bad/under/over), source-colormap non-mutation,TypeErrorfor a colormap object withas_cmap=False, theValueErrorfor an empty palette,n_colorsbeing ignored withas_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)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