1

我想创建一些电磁散射过程的远场图。

为此,我计算了值 θ、φ 和 r。坐标 θ 和 φ 在单位球面上创建了一个规则网格,因此我可以使用plot_Surface在此处找到)转换为笛卡尔坐标。

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

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

4

1 回答 1

1

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

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

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

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()
于 2013-03-26T13:48:18.483 回答