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

如何在Matplotlib极坐标图中绘制基于Attenuation [dB]的两条曲线?

按Attenuation分组绘制极坐标曲线的修改方案

修改后完整代码(推荐用Pandas实现,代码更简洁)

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

# 读取CSV数据
df = pd.read_csv('你的数据文件.csv')

# 按Attenuation [dB]的值分组
attenuation_groups = df.groupby('Attenuation [dB]')

# 创建极坐标画布
fig, ax = plt.subplots(subplot_kw={'projection': 'polar'})

# 遍历每个衰减组,分别绘制曲线
for attn_value, group_data in attenuation_groups:
    # 将角度从度转换为极坐标所需的弧度
    theta = np.deg2rad(group_data['Position [Deg]'])
    # 取吞吐量作为极径值
    throughput = group_data['Traffic Pair 1 Throughput [Mbps]']
    # 绘制曲线,添加标签用于图例区分
    ax.plot(theta, throughput, label=f'衰减值: {attn_value} dB', marker='o')

# 优化极坐标显示设置
ax.set_theta_zero_location('N')  # 将0度位置设为顶部(北方向)
ax.set_theta_direction(-1)       # 角度按顺时针方向递增
ax.set_rlabel_position(90)       # 极径标签放在右侧
ax.set_title('不同衰减下的吞吐量随角度变化', y=1.1)
# 添加图例
ax.legend()

plt.show()

关键改动说明

  1. 数据分组:用pandas.groupby()按Attenuation [dB]列拆分数据,自动把10dB和20dB的数据集分开,避免了手动筛选的繁琐。
  2. 分曲线绘制:遍历每个分组,单独转换角度单位并绘制曲线,通过label参数为每条曲线添加标识,最终用图例区分。
  3. 极坐标优化:添加了常用的极坐标显示设置,让图表更符合常规阅读习惯。

原生CSV模块实现(无需Pandas)

如果你的环境没有安装Pandas,可以用原生CSV模块手动筛选数据:

import matplotlib.pyplot as plt
import numpy as np
import csv

# 初始化存储不同衰减数据的列表
attn10_theta = []
attn10_throughput = []
attn20_theta = []
attn20_throughput = []

# 读取并筛选数据
with open('你的数据文件.csv', 'r') as f:
    reader = csv.DictReader(f)
    for row in reader:
        deg = float(row['Position [Deg]'])
        attn = int(row['Attenuation [dB]'])
        tp = float(row['Traffic Pair 1 Throughput [Mbps]'])
        theta = np.deg2rad(deg)
        
        if attn == 10:
            attn10_theta.append(theta)
            attn10_throughput.append(tp)
        elif attn == 20:
            attn20_theta.append(theta)
            attn20_throughput.append(tp)

# 创建极坐标图并绘制两条曲线
fig, ax = plt.subplots(subplot_kw={'projection': 'polar'})
ax.plot(attn10_theta, attn10_throughput, label='衰减值: 10 dB', marker='o')
ax.plot(attn20_theta, attn20_throughput, label='衰减值: 20 dB', marker='s')

# 图表设置
ax.set_theta_zero_location('N')
ax.set_theta_direction(-1)
ax.set_rlabel_position(90)
ax.set_title('不同衰减下的吞吐量随角度变化', y=1.1)
ax.legend()

plt.show()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 12:15:05