From 97632f31cd2a4b8845d34fab730674ab0eea1410 Mon Sep 17 00:00:00 2001 From: Hafiz Hussain Date: Thu, 23 Jul 2026 15:38:05 +0500 Subject: [PATCH] Stop using deprecated matplotlib get_cmap (#2499) matplotlib.cm.get_cmap() (and plt.cm.get_cmap()) were deprecated in matplotlib 3.7 and removed in 3.9. Several call sites used it as a try/except fallback where the try branch already used the supported plt.get_cmap(name, lut); since matplotlib is unpinned, the fallback branch would itself fail on current matplotlib. Replace with the supported API: - common.py: classify(), get_palette_colors() - drop the deprecated try/except fallbacks - toolbar.py: time_slider() - drop the deprecated try/except fallbacks - cartoee.py: build_palette() - use plt.get_cmap in the old-matplotlib branch and drop the now-unused 'from matplotlib import cm' Behavior is preserved on supported matplotlib versions. black and compile checks pass. Note: scoped to the matplotlib part of #2499; no deprecated traitlets calls were found in a scan, so that can be handled separately. --- geemap/cartoee.py | 4 ++-- geemap/common.py | 10 ++-------- geemap/toolbar.py | 10 ++-------- 3 files changed, 6 insertions(+), 18 deletions(-) diff --git a/geemap/cartoee.py b/geemap/cartoee.py index 0dea81a18b..49475816a0 100644 --- a/geemap/cartoee.py +++ b/geemap/cartoee.py @@ -23,7 +23,7 @@ import matplotlib as mpl import matplotlib.patches as patches import matplotlib.pyplot as plt -from matplotlib import cm, colors +from matplotlib import colors from matplotlib import font_manager as mfonts from matplotlib.lines import Line2D import numpy as np @@ -235,7 +235,7 @@ def build_palette(cmap: str, n: int = 256) -> list[str]: colormap = mpl.colormaps[cmap].resampled(n) else: # For older matplotlib versions - colormap = cm.get_cmap(cmap, n) + colormap = plt.get_cmap(cmap, n) return [colors.rgb2hex(colormap(i / (n - 1) if n > 1 else 0)[:3]) for i in range(n)] diff --git a/geemap/common.py b/geemap/common.py index 2237ec3988..719751f8b0 100644 --- a/geemap/common.py +++ b/geemap/common.py @@ -11919,10 +11919,7 @@ def classify( if cmap is None: cmap = "Blues" - try: - cmap = plt.get_cmap(cmap, k) - except: - cmap = plt.cm.get_cmap(cmap, k) + cmap = plt.get_cmap(cmap, k) if colors is None: colors = [mpl.colors.rgb2hex(cmap(i))[1:] for i in range(cmap.N)] colors = ["#" + i for i in colors] @@ -12676,10 +12673,7 @@ def get_palette_colors( n_class: The number of colors. Defaults to None. hashtag: Whether to return a list of hex colors. Defaults to False. """ - try: - cmap = plt.get_cmap(cmap_name, n_class) - except: - cmap = plt.cm.get_cmap(cmap_name, n_class) + cmap = plt.get_cmap(cmap_name, n_class) colors = [mpl.colors.rgb2hex(cmap(i))[1:] for i in range(cmap.N)] if hashtag: colors = ["#" + i for i in colors] diff --git a/geemap/toolbar.py b/geemap/toolbar.py index 813073d3ba..44a571d945 100644 --- a/geemap/toolbar.py +++ b/geemap/toolbar.py @@ -2548,10 +2548,7 @@ def classes_changed(change): if selected != "Any": n_class = int(classes.value) - try: - colors = plt.get_cmap(colormap.value, n_class) - except Exception: - colors = plt.cm.get_cmap(colormap.value, n_class) + colors = plt.get_cmap(colormap.value, n_class) cmap_colors = [ mpl.colors.rgb2hex(colors(i))[1:] for i in range(colors.N) ] @@ -2635,10 +2632,7 @@ def colormap_changed(change) -> None: if classes.value != "Any": n_class = int(classes.value) - try: - colors = plt.get_cmap(colormap.value, n_class) - except Exception: - colors = plt.cm.get_cmap(colormap.value, n_class) + colors = plt.get_cmap(colormap.value, n_class) cmap_colors = [mpl.colors.rgb2hex(colors(i))[1:] for i in range(colors.N)] _, ax = plt.subplots(figsize=(6, 0.4))