Python: mplot3d, graphing a solid
I'm trying to graph a wireframe solid of revolution. I'm following the example开发者_运维技巧 for a sphere here but I'm a bit at a loss. I've simplified everything down, but am now stuck on an error. I'm also looking at the function arguments described here, but unless I'm misunderstanding something, this code should be okay. I do realize that what I'm trying to draw here is a line and not a shape, but I don't understand why I can't use this method to draw it anyway. I'm trying to get this example as simple as possible so I can move on to graphing an actual solid. Here it is:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plot
import numpy
import pylab
fig = plot.figure()
ax = Axes3D(fig)
n = numpy.linspace(0, 100)
x = n
y = x**2
z = 1
ax.plot_wireframe(x, y, z)
plot.show()
Here's the error:
Traceback (most recent call last):
File "test.py", line 14, in <module>
ax.plot_wireframe(x, y, z)
File "/usr/lib/pymodules/python2.6/mpl_toolkits/mplot3d/axes3d.py", line 687, in plot_wireframe
rows, cols = Z.shape
AttributeError: 'int' object has no attribute 'shape'
When matplotlib writes data arguments in capital letters, that means it's expecting matrices of data. You can use the meshgrid
function (see the example for mplot3d
) to generate the grid.
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plot
import numpy
import pylab
fig = plot.figure()
ax = Axes3D(fig)
n = numpy.linspace(0, 100)
x = n
y = x**2
X, Y = numpy.meshgrid(x, y)
Z = numpy.ones_like( X )
ax.plot_wireframe(X, Y, Z)
Note that in the example you gave, the mesh points for the sphere are constructed using an outer product.
精彩评论