refactor: update colormap API for new versions of matplotlib - #2825
refactor: update colormap API for new versions of matplotlib#2825alepr wants to merge 5 commits into
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a helper function get_colormap in geemap/colormaps.py to unify colormap retrieval across different Matplotlib versions, and refactors geemap/common.py to use it. Feedback suggests that in older Matplotlib versions, plt.colormaps is a function rather than a registry object, which would cause a TypeError when subscripted. It is recommended to check mpl.colormaps instead and gracefully handle cases where cmap_name is already a Colormap instance.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
for more information, see https://pre-commit.ci
There was a problem hiding this comment.
Pull request overview
This PR continues the Matplotlib colormap API migration by centralizing colormap retrieval in geemap/colormaps.py and refactoring colormap-dependent utilities in geemap/common.py to use that shared helper, aiming to avoid deprecated plt.get_cmap() / plt.cm.get_cmap() patterns.
Changes:
- Add
colormaps.get_colormap()as a centralized colormap accessor with version-dependent fallbacks. - Refactor
save_colorbar()andclassify()to use the centralized colormap accessor. - Make
get_palette_colors()more robust by defaultingNonecolormap names and keeping hex formatting consistent.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| geemap/common.py | Switch colormap lookups in save_colorbar, classify, and get_palette_colors to the new centralized helper; add None fallback for cmap_name. |
| geemap/colormaps.py | Introduce get_colormap() intended to bridge old/new Matplotlib colormap APIs. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| cmap_name: The name of the matplotlib colormap. Defaults to None. | ||
| 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) | ||
| if cmap_name is None: |
Description
Following up on my previous PR (#2822), this PR continue the migration to the updated Matplotlib API for colormap access. Resolves #2499
Previously,
geemaprelied onplt.get_cmap()andplt.cm.get_cmap(), which have been deprecated in newer versions of Matplotlib. To address this and ensure long-term compatibility, I have:get_colormapfunction ingeemap/colormaps.pythat handles the API transition gracefully usinghasattrchecks.common.py(specificallysave_colorbar,classify, andget_palette_colors) to use the new centralized function.get_palette_colorsto handleNonevalues and ensure consistent hex color formatting.This change ensures that
geemapremains compatible with the latest Matplotlib releases while cleaning up deprecated code patterns.