You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

各向同性辐射器3D辐射图及2D投影绘制故障排查求助

各向同性辐射器3D图案与2D投影绘图问题

我想要绘制各向同性辐射器的3D辐射图案及其在XY平面或UV空间的2D投影,3D绘图可正常运行,但2D绘图出现问题,反复调试仍未找到错误原因。

原始代码如下:

import matplotlib.pyplot as plt
import numpy as np
pi= np.pi
'''Plot 3D Patterns'''
theta = np.arange(-180,180,0.5, dtype=np.float64) * (pi / 180) # radians
phi = np.arange(-180,180,0.5, dtype=np.float64) * (pi / 180) # radians
theta,phi = np.meshgrid(theta,phi)

element_rad_intensity = 1 #Isotopric
element_x = element_rad_intensity * np.sin(theta) * np.cos(phi) 
element_y = element_rad_intensity * np.sin(theta) * np.sin(phi) 
element_z = element_rad_intensity * np.cos(theta) 
 
fig, ax = plt.subplots(subplot_kw=dict(projection='3d'))
ax.title.set_text('3D Isotropic Radiation Pattern')
surf = ax.plot_surface(element_x,element_y,element_z,cmap = 'gist_earth')
fig.colorbar(surf, shrink=0.5, aspect=5)


'''Plot UV Space or 3D Projection onto 2D XY Plane'''
theta = np.arange(-180,180,0.5, dtype=np.float64) * (pi / 180) # radians
phi = np.arange(-180,180,0.5, dtype=np.float64) * (pi / 180) # radians
x =  np.sin(theta) * np.cos(phi)  #u
y =  np.sin(theta) * np.sin(phi)  #v
x,y = np.meshgrid(x,y) #uv co-ordinates

fig = plt.figure()
ax = fig.add_subplot(111)
ax.title.set_text('2D Isotropic Radiation Projection onto XY or UV Plane')
plt.contourf(x,y,element_x+element_y)
plt.contourf(x,y,element_x+element_y, cmap = 'gist_earth') 
cbar = plt.colorbar()

问题分析

  1. 维度不匹配:第二次定义theta和phi后,未执行meshgrid就直接计算x、y,后续再对x、y执行meshgrid会导致维度混乱,与element_x+element_y的维度不兼容。
  2. 强度参数错误:element_x+element_y并非各向同性辐射在UV平面的强度,各向同性辐射的辐射强度是恒定值1,该计算不符合物理意义。
  3. 重复绘图调用:连续两次调用contourf属于冗余操作,会覆盖前一次的绘图结果。

修正后的代码

import matplotlib.pyplot as plt
import numpy as np
pi = np.pi

'''Plot 3D Patterns'''
theta = np.arange(-180, 180, 0.5, dtype=np.float64) * (pi / 180)  # 转换为弧度
phi = np.arange(-180, 180, 0.5, dtype=np.float64) * (pi / 180)
theta, phi = np.meshgrid(theta, phi)

element_rad_intensity = 1  # 各向同性辐射强度
element_x = element_rad_intensity * np.sin(theta) * np.cos(phi)
element_y = element_rad_intensity * np.sin(theta) * np.sin(phi)
element_z = element_rad_intensity * np.cos(theta)

fig, ax = plt.subplots(subplot_kw=dict(projection='3d'))
ax.title.set_text('3D 各向同性辐射图案')
surf = ax.plot_surface(element_x, element_y, element_z, cmap='gist_earth')
fig.colorbar(surf, shrink=0.5, aspect=5)

'''Plot UV Space or 3D Projection onto 2D XY Plane'''
# 复用已完成meshgrid的theta和phi,避免重复计算
u = np.sin(theta) * np.cos(phi)  # UV空间u分量
v = np.sin(theta) * np.sin(phi)  # UV空间v分量
# 各向同性辐射在UV平面的强度恒定为1
intensity = np.ones_like(u)

fig = plt.figure()
ax = fig.add_subplot(111)
ax.title.set_text('各向同性辐射在XY/UV平面的2D投影')
contour = ax.contourf(u, v, intensity, cmap='gist_earth')
cbar = plt.colorbar(contour)
plt.show()

修改说明

  • 复用3D绘图中已完成meshgrid的theta和phi,确保维度一致。
  • 使用np.ones_like(u)生成恒定强度值,符合各向同性辐射的物理特性。
  • 移除冗余的contourf调用,添加plt.show()确保图像正常显示。

内容的提问来源于stack exchange,提问作者Natalie Johnson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 02:02:19