Matplotlib: “Unknown projection '3d'” error

前端 未结 5 1998
南笙
南笙 2020-12-07 16:00

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_         


        
相关标签:
5条回答
  • 2020-12-07 16:41

    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 讨论(0)
  • 2020-12-07 16:52

    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
    0 讨论(0)
  • 2020-12-07 16:52

    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!

    0 讨论(0)
  • 2020-12-07 16:57

    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.

    0 讨论(0)
  • 2020-12-07 16:58

    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)

    0 讨论(0)
提交回复
热议问题