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

如何用Python绘制3D天线辐射方向图?代码无输出如何调整?

3D天线辐射方向图绘制问题修正方案

核心错误点

  • 角度单位不匹配:读入的theta、phi是角度值,Python三角函数默认要求输入弧度,直接传入角度会导致坐标计算完全失真。
  • RCS值为负:你的RCS是dB单位的负值,直接作为球坐标半径会导致坐标全部反向,无法生成有效图形,需要先转换为正的线性值,或者添加偏移量转为正值。
  • Surface绘图参数格式错误:plotly的go.Surface要求输入x、y为二维网格数组,z为对应二维矩阵,你当前传入的是一维序列,无法生成曲面。
  • 球坐标转换逻辑需要和天线领域的角度定义匹配,避免轴翻转。

修正后可运行代码

import math
import pandas as pd
import numpy as np
import plotly.graph_objects as go


def spherical_to_cartesian(theta_rad, phi_rad, r):
    # theta是俯仰角(与z轴夹角,弧度),phi是方位角(弧度),r是正的半径值
    x = r * math.sin(theta_rad) * math.cos(phi_rad)
    y = r * math.sin(theta_rad) * math.sin(phi_rad)
    z = r * math.cos(theta_rad)
    return x, y, z


def get_transformed_dataframe(path):
    df = pd.read_csv(path, sep="\s+", skiprows=2, header=None)
    df.columns = ['theta', 'phi', 'rcs', 'abs(theta)', 'phase(theta)', 'abs(phi)', 'phase(phi)', 'ax.ratio']
    # 1. 角度转弧度
    df['theta_rad'] = np.deg2rad(df['theta'])
    df['phi_rad'] = np.deg2rad(df['phi'])
    # 2. dB转线性值 保证r为正
    df['r'] = 10 ** (df['rcs'] / 10)
    # 也可以用偏移的方式:df['r'] = df['rcs'] + 60 (只要保证所有r为正即可)
    # 3. 坐标转换
    result_df = df.apply(lambda x: spherical_to_cartesian(x.theta_rad, x.phi_rad, x.r), axis=1, result_type='expand')
    result_df.columns = ['x', 'y', 'z']
    result = df.join(result_df)
    return result


df = get_transformed_dataframe('test.txt')
# 生成二维网格:先对phi和theta去重排序,再生成meshgrid
phi_unique = sorted(df['phi'].unique())
theta_unique = sorted(df['theta'].unique())
# 生成对应二维坐标矩阵
z_grid = df.pivot(index='theta', columns='phi', values='z').values
x_grid = df.pivot(index='theta', columns='phi', values='x').values
y_grid = df.pivot(index='theta', columns='phi', values='y').values

# 绘图
fig = go.Figure(data=[go.Surface(z=z_grid, x=x_grid, y=y_grid, colorscale='Viridis')])
fig.update_layout(title='3D天线辐射方向图',
                  scene=dict(xaxis_title='X', yaxis_title='Y', zaxis_title='Z'))
fig.show()

JS实现说明

如果用JS实现,逻辑完全和上述Python代码一致:

  1. 读取数据后先把角度转弧度
  2. 把dB单位的RCS转为正的线性值
  3. 把一维数据整理为二维网格格式
  4. 调用plotly.js的Surface接口渲染即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 17:36:00