ValueError: Unknown projection '3d' (once again)

Leos313 picture Leos313 · May 20, 2019 · Viewed 21.6k times · Source

When executing this line of code:

import matplotlib.pyplot as plt

#your code

fig = plt.figure()
ax = fig.gca(projection='3d')

I have an output error:

raise ValueError("Unknown projection %r" % projection)

ValueError: Unknown projection '3d'

<Figure size 432x288 with 0 Axes>

The error appears also when I use Spyder as IDE. The version of matplotlib is

print('matplotlib: {}'.format(matplotlib.__version__))
matplotlib: 1.5.0rc3

But I had the same problem even with other versions of matplotlib. A similar error was reported in this question (Stackoverflow) but the answers do not help. Some suggestions on how to modify the instruction? matplotlib: 3.0.2

Answer

Sheldore picture Sheldore · May 20, 2019

You will have to import Axes3D to enable the 3d plotting in matplotlib. The official tutorials on 3d plotting can be found here. So the correct imports and code would look like

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D # <--- This is important for 3d plotting 

#your code

fig = plt.figure()
ax = fig.gca(projection='3d')