Skip to content

Fix matplotlib>=3.9 compatibility: replace removed plt.cm.get_cmap - #205

Merged
CameronBodine merged 1 commit into
CameronBodine:mainfrom
HuggeK:fix/matplotlib-get-cmap
Jul 31, 2026
Merged

Fix matplotlib>=3.9 compatibility: replace removed plt.cm.get_cmap#205
CameronBodine merged 1 commit into
CameronBodine:mainfrom
HuggeK:fix/matplotlib-get-cmap

Conversation

@HuggeK

@HuggeK HuggeK commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #203.

On matplotlib 3.9+, running the test dataset (python -m pingmapper test) crashes during sonogram tile export with:

AttributeError: module 'matplotlib.cm' has no attribute 'get_cmap'

matplotlib.cm.get_cmap was deprecated in matplotlib 3.7 and removed in 3.9. PINGMapper calls it as plt.cm.get_cmap(...) in several colormap paths, so any environment with a current matplotlib (reproduced here on 3.11) fails as soon as it reaches sonogram/rectified-tile colorizing.

Fix

Replace every plt.cm.get_cmap(...) with the still-supported plt.get_cmap(...) (matplotlib.pyplot.get_cmap). This is a minimal drop-in: it returns the same Colormap object, is not deprecated in current matplotlib, and — like the old API — gracefully handles a None colormap name. The already-valid matplotlib.colormaps.get_cmap(...) calls in class_rectObj.py are left untouched.

File Call sites fixed
pingmapper/class_sonObj.py 1 (_colorize_array_batched, the crash in #203)
pingmapper/class_rectObj.py 5 (rectified GeoTiff colormap paths)
pingmapper/class_mapSubstrateObj.py 1 (substrate summary plot)
pingmapper/class_sonObj_nadirgaptest.py 1

Testing

Ran python -m pingmapper test (small dataset) on matplotlib 3.11 — the full read → rectify → substrate pipeline now runs to completion, including the previously-failing "Exporting sonogram tiles" stage.

Why plt.get_cmap rather than matplotlib.colormaps[name]

Both work, but plt.get_cmap(name) is the smallest possible change from plt.cm.get_cmap(name) (just drop .cm), keeps the exact same call signature, and preserves the old behavior of accepting None (returns the default colormap) — whereas matplotlib.colormaps[None] raises KeyError. plt is already imported in every affected module, so no import changes are needed. It also matches the existing colormaps.get_cmap(...) usage already present in class_rectObj.py.


🤖 Generated with Claude Code

matplotlib deprecated `matplotlib.cm.get_cmap` in 3.7 and removed it in
3.9, so on newer matplotlib (reproduced on 3.11) any colormap export
raised:

    AttributeError: module 'matplotlib.cm' has no attribute 'get_cmap'

This broke `python -m pingmapper test` during sonogram tile export
(sonObj._colorize_array_batched) and would also break the rectification
colormap path (rectObj) and the substrate summary plot.

Replace every `plt.cm.get_cmap(...)` call with the still-supported
`plt.get_cmap(...)` (matplotlib.pyplot.get_cmap), a drop-in that also
handles a None colormap name like the old API. The already-valid
`matplotlib.colormaps.get_cmap(...)` calls are left unchanged.

Verified by running `python -m pingmapper test` (small dataset) through
the full read -> rectify -> substrate pipeline to completion on
matplotlib 3.11.

Fixes CameronBodine#203.

Co-authored-by: HuggeK <48095810+HuggeK@users.noreply.github.com>
@HuggeK
HuggeK marked this pull request as ready for review July 29, 2026 13:48
@CameronBodine
CameronBodine merged commit 4e06596 into CameronBodine:main Jul 31, 2026
3 checks passed
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.

" module 'matplotlib.cm' has no attribute 'get_cmap' "

3 participants