2013-03-25 53 views
0

我想创建一些电磁散射过程的远场图。matplotlib中的距离依赖着色

为此,我计算了θ,φ和r值。坐标θ和φ在unitphere上创建一个规则网格,所以我可以使用plot_Surfacefound here)转换为笛卡尔坐标。

我现在的问题是,我需要一种方法来对半径为r而不是高度为z的表面着色,这似乎是默认的。

有没有办法改变这种依赖?

+0

也许这有助于:http:// s tackoverflow.com/questions/6539944/color-matplotlib-plot-surface-command-with-surface-gradient/6543777#6543777 – Paul 2013-03-25 14:02:41

+0

我没有注意到这种可能性。我试图实现这一点。谢谢! – Stefan 2013-03-25 14:19:53

+0

也从Enthought中查看“mayavi”。它是一个不同的框架,但它是围绕openGL构建的,所以在3D渲染方面做得更好。 – tacaswell 2013-03-25 15:14:20

回答

0

我不知道你是怎么做的,所以也许你已经解决了它。但是,基于Paul评论的链接,你可以做这样的事情。我们使用plot_surface的facecolor参数传递我们想要的颜色值。

(我已经从matplotlib文档修改的surface3d演示)

编辑:正如斯蒂芬在他的评论中指出的那样,我的回答可以简化为:

from mpl_toolkits.mplot3d import Axes3D 
import matplotlib.pyplot as plt 
import matplotlib.cm as cm 
import numpy as np 

fig = plt.figure() 
ax = fig.gca(projection='3d') 
X = np.arange(-5, 5, 0.25) 
xlen = len(X) 
Y = np.arange(-5, 5, 0.25) 
ylen = len(Y) 
X, Y = np.meshgrid(X, Y) 
R = np.sqrt(X**2 + Y**2) 
maxR = np.amax(R) 
Z = np.sin(R) 

# Note that the R values must still be normalized. 
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, facecolors=cm.jet(R/maxR), 
     linewidth=0) 

plt.show() 

和(结束)我不必要的复杂的原始版本,使用与上面相同的代码,但省略matplotlib.cm导入,

# We will store (R, G, B, alpha) 
colorshape = R.shape + (4,) 
colors = np.empty(colorshape) 
for y in range(ylen): 
    for x in range(xlen): 
     # Normalize the radial value. 
     # 'jet' could be any of the built-in colormaps (or your own). 
     colors[x, y] = plt.cm.jet(R[x, y]/maxR) 

surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, facecolors=colors, 
     linewidth=0) 

plt.show() 
+1

谢谢!这基本上是我所做的。但是我直接通过'plot_surface'中的'facecolors = cm.jet(rdata)'使用半径。 – Stefan 2013-03-27 12:43:14

+0

啊,当然!更干净。在我的回答中,我会在其他人身上发现这一点。 – Sam 2013-03-27 14:56:49