Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Databases and Redis

Matplotlib refuses gca(projection='3d') in new versions

Your error

TypeError: FigureBase.gca() got an unexpected keyword argument 'projection'1 more line it can show

Fix it fast

Most common cause, 1 of 3

Your own code passes projection to gca()

Ask for the 3D Axes when you create it. add_subplot takes the projection name directly, which is how Matplotlib's own 3D gallery examples start.

Fix
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(projection='3d')
ax.scatter([0, 1, 2], [0, 1, 4], [0, 1, 8])
plt.show()
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

Matplotlib 3.4 deprecated keyword arguments to gca(), and 3.6 removed them with no replacement, so FigureBase.gca() got an unexpected keyword argument 'projection' is a TypeError on every current version. Create the 3D Axes yourself with fig.add_subplot(projection='3d') or plt.subplots(subplot_kw={'projection': '3d'}). If the call sits inside a library, upgrade that library.

  • Applies to Matplotlib 3.11, Matplotlib 3.6 and later, Python 3.11+
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Look at the last frames of the traceback. A file of your own means cause 1; a path inside site-packages means a library still calls the removed form (cause 2).

Run this first
python -c "import matplotlib; print(matplotlib.__version__)" && python your_script.py 2>&1 | grep -n 'File "'

Then find the line you see in this list and open its fix:

  1. 1Your own code passes projection to gca()The traceback's last frame of your code is a line like ax = fig.gca(projection='3d') or plt.gca(projection='3d')
  2. 2A library or notebook you depend on still uses the removed formThe failing gca(projection=...) line is inside site-packages or a downloaded helper, not in your files
  3. 3plt.subplots() was called without asking for 3DAfter removing gca, ax.plot_surface or ax.scatter with three arrays fails because ax is a normal 2D Axes

What does the error look like?

  • TypeError: FigureBase.gca() got an unexpected keyword argument 'projection'
  • TypeError: gca() got an unexpected keyword argument 'projection'

Cause 1 of 3

Your own code passes projection to gca()

You seeThe traceback's last frame of your code is a line like ax = fig.gca(projection='3d') or plt.gca(projection='3d')

Check

The TypeError about gca() and the projection keyword comes from an old recipe. Matplotlib's 3.4 API changes marked keyword arguments to pyplot.gca and Figure.gca as deprecated, and the 3.6 removals list says they are gone with no replacement. gca() now only returns the current Axes.

Check
grep -rn "gca(projection" --include=*.py .

Fix

Ask for the 3D Axes when you create it. add_subplot takes the projection name directly, which is how Matplotlib's own 3D gallery examples start.

Fix
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(projection='3d')
ax.scatter([0, 1, 2], [0, 1, 4], [0, 1, 8])
plt.show()

Verify

The script draws the plot and print(type(ax).__name__) shows Axes3D.

Verify
python -c "import matplotlib.pyplot as plt; ax = plt.figure().add_subplot(projection='3d'); print(type(ax).__name__)"

Confirmed by matplotlib.org

Did this fix it?

Cause 2 of 3

A library or notebook you depend on still uses the removed form

You seeThe failing gca(projection=...) line is inside site-packages or a downloaded helper, not in your files

Check

Matplotlib 3.4 announced that passing keyword arguments to gca would stop working, so packages written before then broke when they met 3.6. Find which package owns the frame and which version you have.

Check
python -m pip show <package-from-the-traceback> | grep -E '^(Name|Version)'

Fix

Upgrade that package to a release that builds its 3D Axes with add_subplot. If no fixed release exists yet, pin Matplotlib below 3.6 in that one environment until it does, and note the pin so it is removed later.

Fix
python -m pip install --upgrade <package-from-the-traceback>
# temporary, only if no fixed release exists:
python -m pip install "matplotlib<3.6"

Verify

The same notebook cell or script runs without the TypeError.

Verify
python your_script.py

Confirmed by matplotlib.org

Did this fix it?

Cause 3 of 3

plt.subplots() was called without asking for 3D

You seeAfter removing gca, ax.plot_surface or ax.scatter with three arrays fails because ax is a normal 2D Axes

Check

plt.subplots() returns ordinary Axes unless you pass a projection. The projection parameter of add_subplot lists the accepted names, and the same keyword can travel through subplot_kw.

Check
python -c "import matplotlib.pyplot as plt; f, a = plt.subplots(); print(type(a).__name__)"

Fix

Pass the projection through subplot_kw so every Axes in the grid is 3D.

Fix
fig, ax = plt.subplots(subplot_kw={'projection': '3d'})
ax.plot_surface(X, Y, Z)

Verify

type(ax).__name__ prints Axes3D and the surface renders.

Verify
python -c "import matplotlib.pyplot as plt; f, a = plt.subplots(subplot_kw={'projection': '3d'}); print(type(a).__name__)"

Confirmed by matplotlib.org

Did this fix it?

How do I stop it coming back?

  • Run tests with warnings turned into errors (python -W error::DeprecationWarning) so deprecations fail in CI before a removal release lands.
  • Pin matplotlib in requirements.txt and read the API changes page for each minor release before raising the pin.
  • When copying a plotting snippet, check that it creates Axes with add_subplot or subplots, not gca with arguments.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: stegnor.com, Menulo, ShopGo.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.