# Fix "AttributeError: module 'matplotlib.cm' has no attribute 'get_cmap'" on Matplotlib 3.9

Yes. An agent replaces cm.get_cmap with the matplotlib.colormaps registry, checks that the colors it returns are unchanged, and updates the pinned requirements.

[Run this on Rush](https://cloud.getrush.ai/run/fix-matplotlib-cm-get-cmap-attribute-error/matplotlib/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

Matplotlib 3.9 removed `matplotlib.cm.get_cmap`. The function had printed a deprecation warning since 3.7: "The get_cmap function was deprecated in Matplotlib 3.7 and will be removed two minor releases later. Use `matplotlib.colormaps[name]` or `matplotlib.colormaps.get_cmap(obj)` instead." Code that reaches it through pyplot, as `plt.cm.get_cmap("jet")`, fails the same way, because `plt.cm` is the same module. Wolfrax/windy, a wind visualisation for the Nordic and Baltic areas, has one such call. Line 9 of `bil.py`, a bilinear interpolation demo that ships with the project, sets `self.cmap = plt.cm.get_cmap("jet")` in the constructor of its `Field` class, so nothing in the file runs past object creation on a current Matplotlib.

The agent starts by installing what the project asks for. `requirements.txt` pins `matplotlib==3.4.3`, `numpy==1.20.3`, `scipy==1.6.3` and `Pillow==8.3.2`, a set from 2021. On a current Python those versions have no prebuilt wheels, so pip falls back to compiling them from source, and the agent records where that stops. This is how most people arrive at the error: the pinned install fails, they install the libraries without versions, and the code meets a Matplotlib two removals newer than the one it was written for.

The replacement is one line, but there are three candidates and the agent picks by what the code does with the result. `matplotlib.colormaps["jet"]` is the registry lookup the warning recommends. `plt.get_cmap("jet")` is a pyplot function that was not removed and reads closest to the old call. Code that passed a second argument, `get_cmap(name, n)`, needs `matplotlib.colormaps[name].resampled(n)`, since the registry lookup takes no size. In `bil.py` the colormap is later called as `self.cmap(fld[x, y], bytes=True)` to turn a value between 0 and 1 into four RGBA bytes for a Pillow image, and all three candidates return an object that supports that call.

It then proves the change is only a rename. It evaluates the old call on a Matplotlib where it still exists and the new one on the current release, at the same sample of input values, and compares the byte tuples. It runs the methods of `Field` far enough to build an image, but saves it to a file: the script's `__main__` block calls `.show()`, which opens a viewer window nobody watches in a sandbox. It also greps for the neighbours removed in the same release, such as `cm.register_cmap`, and checks `windy.py`, the data collector, which imports NumPy, SciPy and requests but not Matplotlib. Finally it replaces the pins with versions it installed and ran.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "This project fails with \"AttributeError: module 'matplotlib.cm' has no attribute 'get_cmap'\". Reproduce it: try to install the pinned requirements on a current Python and record whether they install; if they do not, say which package fails and why, then install current versions of the same packages and run the code until the error appears. Search every Python file and notebook for get_cmap reached through matplotlib.cm, plt.cm or a bare import, and for other colormap functions removed in Matplotlib 3.9 such as register_cmap and unregister_cmap. Replace each get_cmap(name) with matplotlib.colormaps[name], and each get_cmap(name, n) with matplotlib.colormaps[name].resampled(n). Keep the behaviour identical: for each replaced call, compute the colors for a sample of inputs with the old function on a Matplotlib version that still has it and with the new code on the current version, and show that they match. Run the affected scripts without opening a window; where a script calls show(), save the figure or image to a file in a scratch directory and do not commit it. Update the requirements to versions you installed and ran, and state the lowest Matplotlib version the new code works on. Change nothing else. Open a pull request with the reproduction, each replaced line, the color comparison and the output of each script you ran.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

A pull request with a small diff: the colormap lookup on each affected line, and the requirements file. The description shows the failed install of the old pins, the traceback on current Matplotlib, and a short table of input values with the RGBA bytes the old and new lookups return for each. It names the lowest Matplotlib release the new code runs on, which matters if other tools in your environment still hold Matplotlib back.

A reviewer checks any call that had a second argument first, because dropping the size changes how many distinct colors a plot gets and nothing raises when that happens. They check the requirements change against their own deployment: new pins for NumPy and SciPy affect more than plotting. Scripts that only draw to a window cannot be verified by eye in a sandbox, so the description says which outputs were saved to files and compared, and which code paths were only imported.

## Run it on every failed CI run

A plotting error like this usually appears the day a base image or a lockfile refresh brings in a new Matplotlib, and the first place it shows is a red workflow. Add an automation to the Project with a `github` trigger on `workflow_run.failure` and a prompt that reads the failing job, and when the cause is a removed Matplotlib function, makes the replacement and opens a pull request. The failed run is added after the prompt as event text. Runs triggered from forks or by bot accounts do not fire it.

Set the automation's max runs per hour low. A broken dependency fails every branch at once, and one fix is enough. For a repository with no CI, a `schedule` trigger that installs the unpinned requirements each week and runs the scripts does the same job on a timer.

## Why run it on Rush?

Showing that a colormap change is harmless takes two Matplotlib versions side by side: one old enough to still have `get_cmap`, one current. Few people keep both installed. The agent creates the two environments in its sandbox, runs the comparison and puts the numbers in the pull request, so the review is a matter of reading a table and not of trusting that a rename is only a rename. The install attempts and their output are kept in the session.

It also separates two problems that arrive together. The old pins not installing and the code not running on new versions look like one failure from the outside. The run reports each with its own evidence, which tells you whether you need a code change, a requirements change or both.

## Frequently asked questions

### What is the direct replacement for cm.get_cmap?

`matplotlib.colormaps["viridis"]` for a lookup by name. If you have a variable that may be a name, a colormap object or `None`, use `matplotlib.colormaps.get_cmap(obj)`. Both exist from Matplotlib 3.6.

### Is plt.get_cmap removed too?

No. `matplotlib.pyplot.get_cmap` is still there and still accepts a name. Only the function in `matplotlib.cm` went away, which is why `plt.cm.get_cmap` fails while `plt.get_cmap` works.

### My code calls get_cmap('viridis', 8). What now?

Use `matplotlib.colormaps["viridis"].resampled(8)`. The registry returns the full colormap, and `resampled` gives a copy with the number of entries you ask for.

### The error comes from seaborn or another library, not my code. Can the agent still help?

Yes, with a different fix. When the traceback ends inside an installed package, the remedy is a newer release of that package or a cap on Matplotlib below 3.9 until one exists. The agent reads the traceback, tells you which case you are in, and changes the requirements accordingly.

## More Matplotlib jobs

- [Fix "OSError: 'seaborn' is not a valid package style" in Matplotlib](https://getrush.ai/run/fix-seaborn-is-not-a-valid-package-style/matplotlib/)
- [Fix "AttributeError: 'FigureCanvasAgg' object has no attribute 'tostring_rgb'"](https://getrush.ai/run/fix-figurecanvasagg-tostring-rgb-attribute-error/matplotlib/)
- [Fix "TypeError: gca() got an unexpected keyword argument 'projection'" in Matplotlib 3D Plots](https://getrush.ai/run/fix-gca-got-an-unexpected-keyword-argument-projection/matplotlib/)

## More jobs like this: Fix a bug from an alert

- [Fix "TypeError: Missing parameter name at 1" After Upgrading to Express 5](https://getrush.ai/run/fix-missing-parameter-name-at-1/path-to-regexp/)
- [Fix "AttributeError: module 'bcrypt' has no attribute '__about__'" with passlib](https://getrush.ai/run/fix-module-bcrypt-has-no-attribute-about/passlib/)
- [Fix "AttributeError: module 'cv2' has no attribute 'xfeatures2d'" by Moving SIFT to cv2.SIFT_create](https://getrush.ai/run/fix-module-cv2-has-no-attribute-xfeatures2d/opencv-python/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
