Fix colormap handling for Matplotlib 3.11 - #4
Open
HugoFara wants to merge 3 commits into
Open
Conversation
addWhitecm and addColorcm called cm.get_cmap(name, 256), deprecated in
Matplotlib 3.7 and removed in 3.11:
AttributeError: module 'matplotlib.cm' has no attribute 'get_cmap'
setup.py requires matplotlib>=3.10.9 with no upper bound, so a fresh
install resolves to 3.11 and every colour-filled plot raises.
The documented replacement for get_cmap(name, lut) is
matplotlib.colormaps[name].resampled(lut). Two details of the old
behaviour are worth keeping, so this goes through a small helper:
- get_cmap also accepted a Colormap instance and returned it unchanged.
psectionV and friends assign the result back into Lcolormap[i], so a
reused list feeds a Colormap in on the next call.
- resampled() returns a new object, so the set_under/set_over calls in
addColorcm no longer risk mutating the registered colormap.
Verified against matplotlib 3.11.1: colormap by name, Colormap
passthrough, addColorcm, addWhitecm, and a Colormap fed back in.
The same two calls exist in Meso-NH's copies of this module
(src/LIB/Python and examples/integration_cases/hpc/OCEAN_LES/005_python)
and are broken there too.
Matplotlib 3.11 marks both as pending deprecation:
PendingDeprecationWarning: The set_under function will be deprecated
in a future version. Use cmap.with_extremes(under=...) or
Colormap(under=...) instead.
with_extremes returns a new colormap rather than mutating in place,
which also removes the last way addColorcm could modify a Colormap
handed to it by the caller. Available since Matplotlib 3.4, so it is
safe against the declared floor of 3.10.9.
Verified on 3.11.1 and 3.10.9 with warnings raised as errors.
The removed matplotlib.cm.get_cmap mapped name=None to rcParams['image.cmap']. The helper did not, so an explicit None in Lcolormap would raise KeyError where it previously plotted with the default colormap. Lcolormap is seeded with 'gist_rainbow_r' when empty, so None only arrives if a caller passes it, but this keeps the replacement a strict drop-in.
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.
matplotlib.cm.get_cmapwas removed in Matplotlib 3.11, andsetup.pyallowsmatplotlib>=3.10.9uncapped — so a fresh install breaks on every filled plot:Replaced with
matplotlib.colormaps[name].resampled(lut)behind a small helper that keeps the original's handling ofNoneand ofColormapinstances (psectionV/psectionHfeed one back in throughLcolormap[i]). Also swapsset_under/set_over, pending deprecation in 3.11, forColormap.with_extremes.Output is bit-identical, checked on six colormaps of both classes. Verified on 3.11.1 and 3.10.9 with warnings as errors.
Meso-NH vendors a byte-identical copy of this file at
src/LIB/Python/, broken the same way.