Python Matplotlib:“未知投影‘3d’”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3810865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Matplotlib: "Unknown projection '3d'" error
提问by rectangletangle
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?
我刚刚安装了 matplotlib 并试图运行其中一个示例脚本。但是我遇到了下面详述的错误。我究竟做错了什么?
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'
采纳答案by Joe Kington
First off, I think mplot3Dworked a bit differently in matplotlibversion 0.99than it does in the current version of matplotlib.
首先,我认为mplot3D在matplotlib版本0.99中的工作方式与当前版本的matplotlib.
Which version are you using? (Try running: python -c 'import matplotlib; print matplotlib."__version__")
您使用的是哪个版本?(尝试运行: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.
我猜您正在运行 version 0.99,在这种情况下,您需要使用稍微不同的语法或更新到更新版本的matplotlib.
If you're running version 0.99, try doing this instead of using using the projectionkeyword argument:
如果您正在运行 version 0.99,请尝试执行此操作而不是使用projection关键字参数:
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 matplotlib1.0.x, as well, not just 0.99.
这也应该适用于matplotlib1.0.x,而不仅仅是0.99.
回答by bvanlew
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.htmlwhich shows projection='3d' working in combination with the Axes3D import.
只是为了添加到 Joe Kington 的回答(没有足够的评论声望),在http://matplotlib.org/examples/mplot3d/mixed_subplots_demo.html的文档中有一个很好的混合 2d 和 3d 图的例子,它显示了投影 = 3d' 与 Axes3D 导入结合使用。
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
事实上,只要存在 Axes3D 导入行
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)
OP 使用的也可以。(使用 matplotlib 1.3.1 版检查)
回答by Colin Wang
I encounter the same problem, and @Joe Kington and @bvanlew's answer solve my problem.
我遇到了同样的问题,@Joe Kington 和 @bvanlew 的回答解决了我的问题。
but I should add more infomation when you use pycharm and enable auto import.
但是当您使用 pycharm 并启用auto import.
when you format the code, the code from mpl_toolkits.mplot3d import Axes3Dwill auto remove by pycharm.
当您格式化代码时,代码from mpl_toolkits.mplot3d import Axes3D将被 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!
它运作良好!
回答by chetan wankhede
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)
回答by Tushar Sharma
Import mplot3d whole to use "projection = '3d'".
导入 mplot3d 整体以使用“projection = '3d'”。
Insert the command below in top of your script. It should run fine.
在脚本顶部插入以下命令。它应该运行良好。
from mpl_toolkits import mplot3d

