我想画一些电磁散射过程的远场曲线图。
为此,我计算了θ,φ和r的值。坐标θ和φ在单位球面上创建了一个规则网格,因此我可以使用plot_Surface (found here)将坐标转换为笛卡尔坐标。
我现在的问题是,我需要一种方法来根据半径r而不是高度z对曲面进行着色,这似乎是默认的。
有没有办法改变这种依赖关系?
发布于 2013-03-26 21:48:18
我不知道你进展如何,也许你已经解决了。但是,根据Paul评论中的链接,您可以这样做。我们使用plot_surface的facecolor参数来传递我们想要的颜色值。
(我已经修改了matplotlib文档中的surface3d演示)
EDIT:正如斯特凡在他的评论中指出的那样,我的答案可以简化为:
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()https://stackoverflow.com/questions/15616768
复制相似问题