Search code examples
pythonmatplotlibcondaspydermatplotlib-3d

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


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


Solution

  • 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')