140

I just installed matplotlib and am trying to run one of there example scripts. However I run into the error detailed below. What am I doing wrong?

from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.gca(projection='3d')
X, Y, Z = axes3d.get_test_data(0.05)
cset = ax.contour(X, Y, Z, 16, extend3d=True)
ax.clabel(cset, fontsize=9, inline=1)

plt.show()

The error is

Traceback (most recent call last):
  File "<string>", line 245, in run_nodebug
  File "<module1>", line 5, in <module>
  File "C:\Python26\lib\site-packages\matplotlib\figure.py", line 945, in gca
    return self.add_subplot(111, **kwargs)
  File "C:\Python26\lib\site-packages\matplotlib\figure.py", line 677, in add_subplot
    projection_class = get_projection_class(projection)
  File "C:\Python26\lib\site-packages\matplotlib\projections\__init__.py", line 61, in get_projection_class
    raise ValueError("Unknown projection '%s'" % projection)
ValueError: Unknown projection '3d'
4

7 Answers 7

121

First off, I think mplot3D worked a bit differently in matplotlib version 0.99 than it does in the current version of matplotlib.

Which version are you using? (Try running: python -c 'import matplotlib; print matplotlib."__version__")

I'm guessing you're running version 0.99, in which case you'll need to either use a slightly different syntax or update to a more recent version of matplotlib.

If you're running version 0.99, try doing this instead of using using the projection keyword argument:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d, Axes3D #<-- Note the capitalization! 
fig = plt.figure()

ax = Axes3D(fig) #<-- Note the difference from your original code...

X, Y, Z = axes3d.get_test_data(0.05)
cset = ax.contour(X, Y, Z, 16, extend3d=True)
ax.clabel(cset, fontsize=9, inline=1)
plt.show()

This should work in matplotlib 1.0.x, as well, not just 0.99.

8
  • 49
    @dashesy - You'll still need to import the 3d projections: from mpl_toolkits.mplot3d import Axes3D. Afterwards, it should work. Apr 24, 2013 at 2:46
  • 2
    aha, interesting! saw it in the examples but guessed it is for older versions only. Thanks
    – dashesy
    Apr 24, 2013 at 2:49
  • 2
    command for print version for python 3 is python -c 'import matplotlib; print(matplotlib.__version__)' Sep 5, 2017 at 21:06
  • 2
    what about version 2.0.2? Sep 5, 2017 at 21:06
  • 3
    I have this error for python 2.7, matplotlib.__version__ '2.2.3' ..... BECAUSE I was not using from mpl_toolkits.mplot3d import Axes3D... :)
    – ntg
    Jan 7, 2019 at 16:49
70

Just to add to Joe Kington's answer (not enough reputation for a comment) there is a good example of mixing 2d and 3d plots in the documentation at http://matplotlib.org/examples/mplot3d/mixed_subplots_demo.html which shows projection='3d' working in combination with the Axes3D import.

from mpl_toolkits.mplot3d import Axes3D
...
ax = fig.add_subplot(2, 1, 1)
...
ax = fig.add_subplot(2, 1, 2, projection='3d')

In fact as long as the Axes3D import is present the line

from mpl_toolkits.mplot3d import Axes3D
...
ax = fig.gca(projection='3d')

as used by the OP also works. (checked with matplotlib version 1.3.1)

4
  • 4
    This is actually really helpful to know! While the Axes3D import is never explicitly used it enables projection='3d' to be used without error
    – stoves
    Aug 3, 2016 at 14:04
  • 3
    The import statement made it work for me. But I can't help feel icky by running this code... Using a import statement to bootstrap some code that looks totally separated on the surface seems like a bad design
    – Ezbob
    Jan 18, 2017 at 16:43
  • 2
    ax = fig.gca(projection='3d') does work for me. Instead of ax = plt.subplot(111,projection="3d"). I'm using the version of 2.1.0
    – M.Bonjour
    Oct 13, 2017 at 2:16
  • 2
    @Ezbob the whole matplotlib API is just a pain in the ass. I always have to google if I want to create even a basic plot. No other python lib has such a crappy API Apr 15, 2019 at 21:46
14

Import mplot3d whole to use "projection = '3d'".

Insert the command below in top of your script. It should run fine.

from mpl_toolkits import mplot3d
4

I encounter the same problem, and @Joe Kington and @bvanlew's answer solve my problem.

but I should add more infomation when you use pycharm and enable auto import.

when you format the code, the code from mpl_toolkits.mplot3d import Axes3D will auto remove by pycharm.

so, my solution is

from mpl_toolkits.mplot3d import Axes3D
Axes3D = Axes3D  # pycharm auto import
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

and it works well!

1
  • You can also put # noinspection PyUnresolvedReferences before the import. Apr 5, 2020 at 18:02
2

As recommended in other comments I found that importing "axes3d" resolved the issue:

from mpl_toolkits.mplot3d import Axes3D

It seems that this is needed if using an older version of matplotlib.pyplot

0

Try this:

import matplotlib.pyplot as plt
import seaborn as sns
from mpl_toolkits.mplot3d import axes3d

fig=plt.figure(figsize=(16,12.5))
ax=fig.add_subplot(2,2,1,projection="3d")

a=ax.scatter(Dataframe['bedrooms'],Dataframe['bathrooms'],Dataframe['floors'])
plt.plot(a)
0

I got this exact same error but from a different part of the sample script. As such is not totally relevant to the OPs problem but others with this error message in the scripts may find this useful.

Somewhere in the code there were lines -

points = points.to_crs({"init": "EPSG:3857"})

and

city = city.to_crs({"init": "EPSG:3857"})

Which generates a warning:

FutureWarning: '+init=<authority>:<code>' syntax is deprecated. '<authority>:<code>' is the preferred initialization method.

I don't think you can ignore that warning. The updated syntax is detailed here:

https://geopandas.org/en/stable/docs/user_guide/projections.html#setting-a-projection

Once you change those to

points = points.to_crs("EPSG:3857")

and

city = city.to_crs("EPSG:3857")

..the ValueError further down the code goes away.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.