你怎么径向'扫出'一维数组在python中绘制3d图? (代表波函数)

时间:2010-11-11 22:34:15

标签: python 3d matplotlib

实际上我有一个很大的一维高度阵列。作为一个小例子考虑:

u=array([0,1,2,1,0,2,4,6,4,2,1])

与高度相对应的径向值的1D阵列,与u大小相同,例如:

r=array([0,1,2,3,4,5,6,7,8,9,10])

显然用以下方式绘制:

pylab.plot(r,u)

给出了一个很好的2D图。

如何在360度左右扫出这个,以给出3D轮廓/曲面图?

如果你能想象它应该看起来像一系列同心圆形脊,就像原子的波函数一样。

任何帮助都会非常感激!

2 个答案:

答案 0 :(得分:2)

你最好使用比matplotlib更面向3D的东西,在这种情况下......

以下是使用mayavi的快速示例: alt text

from enthought.mayavi import mlab
import numpy as np

# Generate some random data along a straight line in the x-direction
num = 100
x = np.arange(num)
y, z = np.ones(num), np.ones(num)

s = np.cumsum(np.random.random(num) - 0.5)

# Plot using mayavi's mlab api
fig = mlab.figure()

# First we need to make a line source from our data
line = mlab.pipeline.line_source(x,y,z,s)

# Then we apply the "tube" filter to it, and vary the radius by "s"
tube = mlab.pipeline.tube(line, tube_sides=20, tube_radius=1.0)
tube.filter.vary_radius = 'vary_radius_by_scalar'

# Now we display the tube as a surface
mlab.pipeline.surface(tube)

# And finally visualize the result
mlab.show()

答案 1 :(得分:2)

#!/usr/bin/python

from mpl_toolkits.mplot3d import Axes3D
import matplotlib
import numpy as np
from scipy.interpolate import interp1d
from matplotlib import cm
from matplotlib import pyplot as plt
step = 0.04
maxval = 1.0
fig = plt.figure()
ax = Axes3D(fig)  



u=np.array([0,1,2,1,0,2,4,6,4,2,1])
r=np.array([0,1,2,3,4,5,6,7,8,9,10])
f=interp1d(r,u)

# walk along the circle
p = np.linspace(0,2*np.pi,50)
R,P = np.meshgrid(r,p)
# transform them to cartesian system
X,Y = R*np.cos(P),R*np.sin(P)

Z=f(R)

ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.jet)
ax.set_xticks([])
plt.show()