Fix Module `matplotlib.cm` Has No Attribute `get_cmap`

This error usually means code written for an older Matplotlib colormap API is running with a newer Matplotlib release where matplotlib.cm.get_cmap() is no longer available. Confirm that the Python interpreter running your code is importing the Matplotlib version you expect, then use a colormap lookup supported by that installation.

Symptom Likely Cause Fix
AttributeError: module 'matplotlib.cm' has no attribute 'get_cmap' Your code or a dependency calls the removed cm.get_cmap() API Use matplotlib.colormaps["viridis"] or update the dependency
AttributeError: 'ColormapRegistry' object has no attribute 'get_cmap' Code expects a registry method unavailable in the installed Matplotlib release Use indexed lookup, such as matplotlib.colormaps["viridis"], for known names
The error occurs only in Jupyter, an IDE, or a deployed app The running interpreter differs from the one where packages were installed Check the interpreter path and Matplotlib import location
The error is inside site-packages A third-party package has incompatible Matplotlib requirements Upgrade that package or use dependency versions it supports

What the matplotlib.cm.get_cmap Error Means

matplotlib.cm is Matplotlib’s colormap module. In older code, this pattern was common:

from matplotlib import cm
cmap = cm.get_cmap("viridis")

A colormap is the rule Matplotlib uses to turn numeric values into colors. For example, viridis maps lower values to dark purple and higher values to yellow-green. The cmap argument used by plotting functions can often take the colormap name directly, so you do not always need to retrieve a colormap object yourself.

The error is about a missing Python attribute, not an invalid colormap name. If "viridis" were misspelled or unavailable, Matplotlib would reach the lookup stage and raise a colormap-name error instead. Here, Python cannot find the get_cmap function on the imported matplotlib.cm module at all.

The object named in the traceback determines the repair. An error mentioning matplotlib.cm calls for a different change than one mentioning ColormapRegistry.

How to Fix matplotlib.cm.get_cmap Without Downgrading

Step 1: Confirm the Matplotlib Copy Your Program Is Using

Run this in the same terminal, virtual environment, notebook kernel, or application runtime that raises the exception:

python -c "import matplotlib; print(matplotlib.__version__); print(matplotlib.__file__)"

Also check the package installation associated with that Python executable:

python -m pip show matplotlib

The reported file path should point into the environment you expect. If it points into your project directory, check for files or folders named matplotlib.py or matplotlib, which can shadow the real package.

In a notebook, run this in a cell:

import sys
import matplotlib
print(sys.executable)
print(matplotlib.__version__)
print(matplotlib.__file__)

I reproduce this kind of mismatch in a clean Python virtual environment because it separates project dependencies from the interpreter that is actually running.

Step 2: Replace cm.get_cmap() With the Colormap Registry

For a fixed, known colormap name, replace the old code:

from matplotlib import cm
cmap = cm.get_cmap("viridis")

with this:

import matplotlib as mpl
cmap = mpl.colormaps["viridis"]

Pass cmap to plotting functions exactly as before:

image = ax.imshow(data, cmap=cmap)

The bracket form is the clearest replacement when you know the colormap name. It also makes failures easier to diagnose: an unknown name fails at the name lookup instead of being mistaken for a missing API method.

Step 3: Update Shared Colormap Helper Functions

A project may have a helper that accepts either a colormap name or a colormap object. In that case, preserve that behavior rather than assuming every value is a string.

import matplotlib as mpl
from matplotlib.colors import Colormap
def resolve_cmap(value):
    if isinstance(value, Colormap):
        return value
    if hasattr(mpl, "colormaps"):
        return mpl.colormaps[value]
    from matplotlib import cm
    return cm.get_cmap(value)

This helper accepts a named colormap such as "viridis" and an existing Colormap instance. It uses the registry when available and retains the legacy path only for older Matplotlib installations that do not provide matplotlib.colormaps.

Do not catch every exception and silently fall back to another API. A nonexistent name should remain an error; otherwise, a typo such as "virids" can become harder to find.

Step 4: Run a Small Plot Test

After changing the lookup, test an actual plot rather than stopping after the import succeeds.

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
data = np.arange(100).reshape(10, 10)
fig, ax = plt.subplots()
image = ax.imshow(data, cmap=mpl.colormaps["viridis"])
fig.colorbar(image, ax=ax)
plt.show()

If this displays a colored grid and color bar without an exception, the colormap lookup works. Run your project’s plotting tests or the original script next, because a dependency may contain additional uses of cm.get_cmap().

Use the Right Colormap API for the Job

For many plots, use a colormap name directly and skip manual lookup:

ax.scatter(x, y, c=values, cmap="viridis")
ax.imshow(data, cmap="magma")

This is often the simplest choice because Matplotlib resolves the name internally.

Use mpl.colormaps["name"] when you need the actual colormap object, for example, to pass it through a utility function, inspect it, or customize its behavior.

import matplotlib as mpl
cmap = mpl.colormaps["plasma"]

Reversed built-in colormaps use the _r suffix:

import matplotlib as mpl
cmap = mpl.colormaps["viridis_r"]

For a custom colormap, register it and retrieve it using the registered key:

import matplotlib as mpl
mpl.colormaps.register(my_cmap, name="project_heat")
cmap = mpl.colormaps["project_heat"]

Keep the registration key and the lookup key identical in project code. Older colormap patterns could be confusing when a colormap’s internal .name differed from the name used during registration.

Fix Dependency and Environment Mismatches

When the failing line is in your own code, changing it to the registry API is usually the durable repair. When the failing line is inside a package under site-packages, your application code may be fine while a dependency is not.

Update the Package That Calls the Old API

Look at the traceback frame immediately above the Matplotlib error. If it points to a package such as a visualization, data-science, or plotting extension, update that package through your project’s normal dependency workflow.

A package upgrade can include the API change needed for current Matplotlib releases. Update the package that owns the failing file rather than editing files directly in site-packages; local edits disappear during the next install and do not solve the underlying compatibility requirement.

Use a Compatible Environment When You Cannot Update Yet

If an application is pinned to an older dependency stack, use the Matplotlib version range supported by that application’s declared requirements. This is a temporary compatibility choice, not the preferred long-term code fix.

Create the environment from the project’s dependency file or lockfile, install its declared packages together, and rerun the original plotting command. Avoid changing only Matplotlib in isolation: plotting libraries, notebook tools, and scientific packages can have connected version constraints.

A reverse mismatch can also happen. Code that calls matplotlib.colormaps.get_cmap(...) may fail in an environment whose ColormapRegistry does not provide that method. For a known string name, indexed lookup is more broadly straightforward:

cmap = mpl.colormaps["viridis"]

Confirm the Fix and Prevent It From Returning

Use this checklist before closing the issue:

  • The traceback no longer contains cm.get_cmap.
  • matplotlib.__file__ points to the intended virtual environment or interpreter.
  • A real plot renders with the desired colormap.
  • Your dependency file records the working package set.
  • Shared plotting utilities use matplotlib.colormaps["name"] for named lookups.
  • Third-party packages that call deprecated APIs have been updated where possible.

Be careful when copying old examples from search results. One widely indexed Matplotlib colormap documentation page contains cm.get_cmap() examples but is explicitly labeled as documentation for an unreleased development build. Check the installed package and the object in your traceback before choosing an API from an example.

Frequently Asked Questions

What Is cmap in Matplotlib?

cmap means colormap: a mapping from numeric data values to colors. Plotting functions such as imshow, scatter, and contourf accept a colormap name like "viridis" or a Colormap object. For ordinary plotting, cmap="viridis" is usually enough.

Is It get_map or get_cmap?

The historical Matplotlib function name is get_cmap, not get_map. get_map is not a Matplotlib colormap lookup API. In current code, prefer matplotlib.colormaps["viridis"] instead of either cm.get_cmap() or a nonexistent get_map() call.

Can I Still Use plt.get_cmap()?

Yes, plt.get_cmap("viridis") is a pyplot-level convenience when your code already uses matplotlib.pyplot. It does not fix a traceback caused by matplotlib.cm.get_cmap, because that code still calls a different module. For reusable library code and explicit named lookups, matplotlib.colormaps["viridis"] is the clearer modern form.

Why Does the Error Happen Only in Jupyter?

Jupyter can run a different Python interpreter from the terminal where you installed or upgraded Matplotlib. Check sys.executable and matplotlib.__file__ inside the notebook kernel; both must point to the environment containing the packages you intended to use. Restart the kernel after changing packages so it does not retain old imports.

Should I Downgrade Matplotlib to Fix This Error?

Downgrading is usually a fallback, not the first repair. Update your own cm.get_cmap() calls or upgrade the dependency that contains them first. If a package cannot yet support your installed Matplotlib release, create an environment using that package’s documented dependency range, then update the package when a compatible release is available.