Skip to content

refactor: update colormap API for new versions of matplotlib - #2825

Open
alepr wants to merge 5 commits into
gee-community:masterfrom
alepr:Fix/colormap-deprecation
Open

refactor: update colormap API for new versions of matplotlib#2825
alepr wants to merge 5 commits into
gee-community:masterfrom
alepr:Fix/colormap-deprecation

Conversation

@alepr

@alepr alepr commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

Description

Following up on my previous PR (#2822), this PR continue the migration to the updated Matplotlib API for colormap access. Resolves #2499

Previously, geemap relied on plt.get_cmap() and plt.cm.get_cmap(), which have been deprecated in newer versions of Matplotlib. To address this and ensure long-term compatibility, I have:

  • Centralized colormap access: Created a new get_colormap function in geemap/colormaps.py that handles the API transition gracefully using hasattr checks.
  • Refactored dependent functions: Updated common.py (specifically save_colorbar, classify, and get_palette_colors) to use the new centralized function.
  • Added robustness: Improved get_palette_colors to handle None values and ensure consistent hex color formatting.

This change ensures that geemap remains compatible with the latest Matplotlib releases while cleaning up deprecated code patterns.

Copilot AI review requested due to automatic review settings July 10, 2026 23:27

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread geemap/colormaps.py Outdated

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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() and classify() to use the centralized colormap accessor.
  • Make get_palette_colors() more robust by defaulting None colormap 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.

Comment thread geemap/colormaps.py
Comment thread geemap/colormaps.py Outdated
Comment thread geemap/common.py
Comment on lines 12674 to +12678
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:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Stop using deprecated matplotlib and traitlets calls

2 participants