ImportError: cannot import name 'get_cmap' from 'matplotlib.cm'
Third-person routing text. Fixes ImportError for get_cmap from matplotlib.cm: matplotlib 3.9 removed it in favor of matplotlib.colormaps and pyplot.get_cmap. Update the import or upgrade the dependent library. Use when an agent hits this with matplotlib 3.9+. Not for other colormap errors.
TL;DR: matplotlib 3.9 removed getcmap from matplotlib.cm. Use `matplotlib.pyplot.getcmap(name) or matplotlib.colormaps[name]` instead, or upgrade the library that does the old import.
The error
ImportError: cannot import name 'get_cmap' from 'matplotlib.cm'Fix it
Replace the import in your code:
from matplotlib import colormaps cmap = colormaps["viridis"]or keep the function style:
from matplotlib.pyplot import get_cmapExpected: no import error.
If the bad import lives inside a third-party package (like an old seaborn-image), upgrade that package:
pip install -U seaborn-imageExpected: new release uses the new path.
Verify:
python -c "from matplotlib import colormaps; print(colormaps['viridis'])"Expected: prints the colormap object.
When this applies
Use this when code does from matplotlib.cm import get_cmap on matplotlib 3.9+.
When it does not apply
If the error is about a different name in matplotlib.cm, check the 3.9 removal list. If matplotlib itself will not import, reinstall it.
Compatibility
matplotlib 3.9+. matplotlib.colormaps exists since 3.5, so the new code works on 3.5 through current.
Root cause
matplotlib deprecated getcmap/getcmap_cls in matplotlib.cm and removed them in 3.9, consolidating colormap lookup in the colormaps registry. Code that imported from the old location breaks at import.
Edge cases
Old tutorials are full of plt.get_cmap; that path still works because pyplot re-exports it. Only the direct from matplotlib.cm import get_cmap form breaks.