# Fix "TypeError: gca() got an unexpected keyword argument 'projection'" in Matplotlib 3D Plots

Yes. An agent finds every gca() call that still passes projection, moves each to add_subplot or plt.axes, and renders every affected figure headlessly to prove it.

[Run this on Rush](https://cloud.getrush.ai/run/fix-gca-got-an-unexpected-keyword-argument-projection/matplotlib/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

Matplotlib 3.4 deprecated calling `gca()` with keyword arguments and 3.6 removed them, so the idiom every 3D tutorial used for a decade, `fig.gca(projection='3d')`, now raises `TypeError: FigureBase.gca() got an unexpected keyword argument 'projection'` on the figure method and `gca() got an unexpected keyword argument 'projection'` on `plt.gca`. The 3.4 and 3.5 releases print `MatplotlibDeprecationWarning: Calling gca() with keyword arguments was deprecated in Matplotlib 3.4. Starting two minor releases later, gca() will take no keyword arguments`, which names the replacements: `plt.axes()` or `plt.subplot()`, and on a figure, `fig.add_subplot()`. The 3D toolkit import that usually sits above the call, `from mpl_toolkits.mplot3d import Axes3D`, is unrelated and still works.

A tutorial repository like xianhu/LearnPython carries the whole history in one file. `python_visual.py` is fifteen demo functions, each building a figure and ending in `plt.show()`, with the call to each commented out below it. `three_dimension_line` on line 456 and `three_dimension_bar` on line 489 create their axes with `fig.gca(projection="3d", title="plot title")`, while `three_dimension_scatter` a few lines above already uses `fig.add_subplot(111, projection="3d")` with the old form left as a comment on line 423, so the file shows both spellings side by side. The same file imports `matplotlib.mlab` and calls `mlab.normpdf` with a `normed` keyword in `histograms_plot`, two more removals from the 3.x line, and hard-codes a macOS font path on line 15.

The agent runs each function under the Agg backend, where `plt.show()` is a no-op, in two environments. On the current release the two 3D functions raise the TypeError and the histogram function fails on `normed`; on Matplotlib 3.5.3 all three run, the 3D ones with the deprecation warning on lines 456 and 489 that says what to change. That pins the boundary between the release that warns and the one that fails, and the warning text is quoted in the pull request so a reader with an older pin knows what is coming. The agent greps the repository for every `gca(` with arguments and every `plt.gca(` and `plt.subplot(` that passes `projection`, since the removal covers all of them.

The port is mechanical: `fig.gca(projection="3d", title="plot title")` becomes `fig.add_subplot(projection="3d", title="plot title")`, which accepts the same keywords because both forward them to the axes constructor, and a bare `plt.gca(projection="3d")` becomes `plt.axes(projection="3d")`. A call that meant to fetch an existing 3D axes rather than create one is the case to read carefully, since `add_subplot` always creates; the agent checks each site for an earlier axes on the same figure. Then it proves the change by rendering: every affected function runs under Agg with `savefig` to a file, the written images are listed with their sizes, and the figure count after each call shows one axes was created, not two. The histogram function's `normpdf` and `normed` are reported with their replacements, `scipy.stats.norm.pdf` and `density=True`, as a separate item.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "This project fails with TypeError: gca() got an unexpected keyword argument 'projection'. In a clean virtual environment install the current Matplotlib and run the code that fails under the Agg backend, quoting the traceback. In a second environment install Matplotlib 3.5 and run the same code with warnings enabled; quote the deprecation warning with its file and line. Grep the repository for every gca( call with arguments, every plt.gca( with arguments, and every plt.subplot( that passes projection. For each, decide whether the code creates a new axes or expected to fetch an existing one, and port it: fig.add_subplot(...) or plt.axes(...) with the same keywords for creation, and a plain gca() after the axes already exists for fetching. Render every function or script you changed under Agg with savefig to a file, list the files written with their sizes, and report the number of axes on each figure. List any other removed Matplotlib API you hit on the way, such as mlab functions or the normed keyword, with replacements, as a separate item. Do not pin Matplotlib to an old release. Open a pull request with both outputs, the diff and the list of rendered files.",
  "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 that changes each `gca` call with keywords to `add_subplot` or `plt.axes`, and nothing else in those functions. The description opens with the traceback from the current release and the deprecation warning from 3.5, each with the file and line, so a reviewer sees the boundary and the wording Matplotlib itself used to announce the change.

Below that is the list of rendered files, one per changed function, with sizes, and the axes count per figure, which is the check that a call meant to reuse an axes did not quietly create a second one. The session's record keeps both environments and every command.

The description ends with the other removed names the agent met in the same files, each with its replacement and marked as not changed in this pull request, so the reviewer can take them next. For a tutorial repository it also notes anything that makes the file machine-specific, such as a hard-coded font path, without changing it.

## Run it on every failed workflow run on a Matplotlib upgrade

This error arrives with a dependency bump past 3.6, or with a fresh environment on a project that never pinned Matplotlib. Add an automation to the Project with the prompt above and a `github` trigger on `workflow_run.failure`, so a job that renders figures and fails on this TypeError starts a run with the failed workflow placed after the prompt as the event. A failure with another cause gets a short report and no pull request.

A plotting script with no CI can use a `schedule` trigger once a month with the same prompt, installing the current Matplotlib in a fresh sandbox and rendering each figure under Agg. A run that finds nothing to fix ends without a pull request and says so, and the automation's max runs per hour keeps a repository full of notebooks from starting many runs at once.

## Why run it on Rush?

The edit is one word per call, and the only way to be sure is to draw. A plotting change that raises is easy; one that silently creates a second axes on a figure, or picks up a different projection than the author expected, only shows in the rendered image. The agent renders every changed function in its own sandbox under a headless backend and reports the files and the axes counts, so the pull request carries evidence rather than a diff that merely imports.

It also settles the version question with the library's own words. The deprecation warning from the 3.4 and 3.5 releases is quoted from a real run, so a team with a pin somewhere between 3.4 and 3.6 sees that the change is coming for them too and can take the same pull request before the bump that breaks them.

## Frequently asked questions

### Which Matplotlib release removed the keyword?

3.6, released in September 2022. 3.4 and 3.5 accept `projection` on `gca()` and print a deprecation warning naming the replacements. The agent runs the code on both sides of that line and quotes what each release says.

### Is add_subplot the same as gca(projection='3d')?

For creating an axes, yes: both forward keywords to the axes constructor, so `title`, `projection` and the rest carry over. The difference is that `gca` returned an existing axes when the figure already had one, while `add_subplot` always creates. The agent checks each call site for that case.

### Do I still need to import Axes3D?

Not since Matplotlib 3.2, where the 3D projection registers itself when `matplotlib.pyplot` is imported. The import still works and is harmless, so the agent leaves it unless the pull request is also removing unused imports.

### What about mlab.normpdf and the normed keyword?

Both were removed in the 3.x line as well: `mlab.normpdf` is `scipy.stats.norm.pdf`, and `normed=True` on a histogram is `density=True`. They fail in the same files for the same reason and the agent lists them with replacements, but they are a separate change from the axes call.

## More Matplotlib jobs

- [Fix "AttributeError: module 'matplotlib.cm' has no attribute 'get_cmap'" on Matplotlib 3.9](https://getrush.ai/run/fix-matplotlib-cm-get-cmap-attribute-error/matplotlib/)
- [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/)

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

- [Fix "RuntimeError: generator raised StopIteration" in Python 3.7 and Later](https://getrush.ai/run/fix-generator-raised-stopiteration/python/)
- [Fix `Call to undefined function get_magic_quotes_gpc()` When a PHP App Lands on PHP 8](https://getrush.ai/run/fix-get-magic-quotes-gpc-removed/php/)
- [Fix "TypeError: glob is not a function" after upgrading the glob package](https://getrush.ai/run/fix-glob-is-not-a-function/glob/)

## 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/)
