python - matplotlib 中距离相关的着色

标签 python matplotlib

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

为此,我计算了 θ、φ 和 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/

相关文章:

python - 在使用 .yaml 配置文件进行日志记录模块时显示请求模块日志记录消息

python - 使用 Matplotlib 在一张条形图中绘制两个字典

python - 当我尝试使用 Python 进行网络抓取表格时,为什么要乘以文本?

python - 将列表列表中的值映射到 Pandas 数据框列

python - 列表不在类定义内的方法之间传递

python - 使用子图而不是 matplotlib 中的图形来控制鼠标单击事件

python - matplotlib + PyQt5 : replace a canvas

python - Pyplot 逐行颜色图

python - 如果刻度标签被修改,Matplotlib 将不会显示任何读数

python - Django Admin 批量编辑多对多关系