views:

44

answers:

1

I'm trying to use mplot3d. I installed matibplot using the Ubuntu (lucid) repositories and it seems broken out-of-the-box. Any help would be appreciated.

This is the code I'm running:

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()

And this is the error that I get:

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'

Thanks.

A: 

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:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = Axes3D(fig)

Second, the code you posted doesn't work even when mplot3D is set up properly.

Try a simpler example. E.g.:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = Axes3D(fig)
plt.show()

Edit: Actually your posted example code does work with matplotlib 0.99 if you replace ax = fig.add_subplot... with ax = Axes3D(fig). However it doesn't seem work with matplotlib 1.0, either way... Not sure what the problem is...

Joe Kington
Thanks. I'm running 0.99.1.1. The first change fixed it.
dannycab