我想创建一些电磁散射过程远场图。
为此,我计算了 θ、φ 和 r 值。坐标 θ 和 φ 在单位球上创建规则网格,因此我可以使用 plot_Surface
( found here ) 转换为笛卡尔坐标。
我现在的问题是,我需要一种相对于半径 r 而不是高度 z 来为表面着色的方法,这似乎是默认值。
有没有办法改变这种依赖关系?
最佳答案
我不知道你进展如何,所以也许你已经解决了这个问题。但是,根据保罗评论中的链接,你可以做这样的事情。我们使用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()
关于python - matplotlib 中距离相关的着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15616768/