mplot3d 在 Ubuntu 10.04 中损坏
我正在尝试使用mplot3d这个工具。我通过Ubuntu的官方软件库安装了matplotlib,但一开始就发现它有问题。希望能得到一些帮助。
这是我运行的代码:
from __future__ import division
from mpl_toolkits.mplot3d import Axes3D
from random import *
from scipy import *
import matplotlib.pyplot as plt
locA = mat([0,0,0])
locB = mat([2,0,0])
locC = mat([1,sqrt(3),0])
locD = mat([1,sqrt(3)/2,sqrt(3)])
startLoc = locA
points = startLoc
n = 10000
x = linspace(1,n,n)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
for i in x:
j = randint(1,4)
if j < 2:
startLoc = (startLoc+locA)/2
points = concatenate((points,startLoc))
elif j < 3:
startLoc = (startLoc+locB)/2
points = concatenate((points,startLoc))
elif j < 4:
startLoc = (startLoc+locC)/2
points = concatenate((points,startLoc))
else:
startLoc = (startLoc+locD)/2
points = concatenate((points,startLoc))
ax.scatter(points[:,0],points[:,1],points[:,2])
plt.show()
这是我遇到的错误:
Traceback (most recent call last):
File "triangle_random_3D.py", line 17, in <module>
ax = fig.add_subplot(111, projection='3d')
File "/usr/lib/pymodules/python2.6/matplotlib/figure.py", line 677, in add_subplot
projection_class = get_projection_class(projection)
File "/usr/lib/pymodules/python2.6/matplotlib/projections/__init__.py", line 61, in get_projection_class
raise ValueError("Unknown projection '%s'" % projection)
ValueError: Unknown projection '3d'
谢谢。
1 个回答
1
首先,我觉得在matplotlib的版本0.99
中,mplot3D的工作方式和现在的版本有点不同。
你现在用的是哪个版本呢?(可以试着运行:python -c 'import matplotlib; print matplotlib.__version__'
)
我猜你可能在用0.99
版本,这样的话你需要用稍微不同的写法,或者更新到更近的matplotlib版本。
如果你在用0.99
版本,可以试试这样做:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = Axes3D(fig)
其次,你发的代码即使在mplot3D
设置正确的情况下也不工作。
试试一个简单点的例子。例如:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = Axes3D(fig)
plt.show()
补充:其实你发的示例代码在matplotlib 0.99中是可以工作的,只要把ax = fig.add_subplot...
替换成ax = Axes3D(fig)
。不过在matplotlib 1.0中似乎就不行了,不知道问题出在哪里...