From d9417c597df3cf420d956b29189940cdf9de7816 Mon Sep 17 00:00:00 2001 From: Hugo Farajallah Date: Thu, 13 Aug 2026 11:35:49 +0200 Subject: [PATCH 1/3] Replace matplotlib.cm.get_cmap, removed in Matplotlib 3.11 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. --- src/MNHPy/Panel_Plot.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/MNHPy/Panel_Plot.py b/src/MNHPy/Panel_Plot.py index b505034..6b4761a 100644 --- a/src/MNHPy/Panel_Plot.py +++ b/src/MNHPy/Panel_Plot.py @@ -11,13 +11,25 @@ import matplotlib as mpl # mpl.use('Agg') import matplotlib.pyplot as plt -from matplotlib import cm -from matplotlib.colors import ListedColormap +from matplotlib.colors import Colormap, ListedColormap import numpy as np import cartopy import cartopy.feature as cfeature +def get_cmap(colormap_in, lut): + """Return colormap_in resampled to lut colors + + Replaces matplotlib.cm.get_cmap(name, lut), deprecated in Matplotlib 3.7 + and scheduled for removal in 3.11. As get_cmap did, a Colormap instance is + returned unchanged; resampled() returns a new object, so the caller may + mutate it without touching the registered colormap. + """ + if isinstance(colormap_in, Colormap): + return colormap_in + return mpl.colormaps[colormap_in].resampled(lut) + + class PanelPlot(): def __init__(self, nb_l, nb_c, Lfigsize, bigtitle, bigtitlepad=0.95, titlepad=40, minmaxpad=1.03, timepad=-0.06, lateralminmaxpad=0.86, @@ -111,7 +123,7 @@ def addWhitecm(self, colormap_in, nb_level, whiteTop=False): """ Add a white color at the top (whiteTop=True) or bottom of the colormap w.r.t. the number of independent colors used """ - color_map = cm.get_cmap(colormap_in, 256) + color_map = get_cmap(colormap_in, 256) newcolor_map = color_map(np.linspace(0, 1, 256)) whites = np.array([1, 1, 1, 1]) # RBG code + opacity if whiteTop: @@ -127,7 +139,7 @@ def addColorcm(self, colormap_in): """ Assign the last color of the colormap to the values out of range """ - colormap = cm.get_cmap(colormap_in, 256) + colormap = get_cmap(colormap_in, 256) colormap.set_under(color=colormap(1./256)) colormap.set_over(color=colormap(1.-1./256)) From 9e5a8b247014852cfdf9242f4989c593c1bfb0ad Mon Sep 17 00:00:00 2001 From: Hugo Farajallah Date: Thu, 13 Aug 2026 11:36:20 +0200 Subject: [PATCH 2/3] Use Colormap.with_extremes instead of set_under/set_over 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. --- src/MNHPy/Panel_Plot.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/MNHPy/Panel_Plot.py b/src/MNHPy/Panel_Plot.py index 6b4761a..b780429 100644 --- a/src/MNHPy/Panel_Plot.py +++ b/src/MNHPy/Panel_Plot.py @@ -141,9 +141,7 @@ def addColorcm(self, colormap_in): """ colormap = get_cmap(colormap_in, 256) - colormap.set_under(color=colormap(1./256)) - colormap.set_over(color=colormap(1.-1./256)) - return(colormap) + return colormap.with_extremes(under=colormap(1./256), over=colormap(1.-1./256)) def set_Title(self, ax, i, title, Lid_overlap, xlab, ylab): """ From a2f8f651fb1bfae7c5740ee1d249e4f57926779d Mon Sep 17 00:00:00 2001 From: Hugo Farajallah Date: Thu, 13 Aug 2026 11:54:59 +0200 Subject: [PATCH 3/3] Keep get_cmap's handling of colormap_in=None 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. --- src/MNHPy/Panel_Plot.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/MNHPy/Panel_Plot.py b/src/MNHPy/Panel_Plot.py index b780429..ce35306 100644 --- a/src/MNHPy/Panel_Plot.py +++ b/src/MNHPy/Panel_Plot.py @@ -21,10 +21,13 @@ def get_cmap(colormap_in, lut): """Return colormap_in resampled to lut colors Replaces matplotlib.cm.get_cmap(name, lut), deprecated in Matplotlib 3.7 - and scheduled for removal in 3.11. As get_cmap did, a Colormap instance is - returned unchanged; resampled() returns a new object, so the caller may - mutate it without touching the registered colormap. + and removed in 3.11. Both special cases of the original are kept: None + means the rcParams default, and a Colormap instance is returned unchanged. + resampled() returns a new object, so the caller may mutate it without + touching the registered colormap. """ + if colormap_in is None: + colormap_in = mpl.rcParams['image.cmap'] if isinstance(colormap_in, Colormap): return colormap_in return mpl.colormaps[colormap_in].resampled(lut)