首页 > 解决方案 > 如何用Python编程绘制wifi信道图

问题描述

嗨,我是 python 图的初学者

我有一些像这样的wifi数据(Dataframe)

SSID 接收信号强度指数 渠道 r_range l_range 带宽
ASUS_Kurt -76 2 0.0 4.0 20MHz
Javen-2.4G -91 3 1.0 9.0 40MHz
-62 6 4.0 8.0 20MHz
wcs2.4G -64 11 9.0 13.0 20MHz
LIN-EXT -74 11 5.0 13.0 40MHz

如何使用 matplotlib 或 seaborn 绘制这样的 Wifi-Channel 图

在此处输入图像描述

我尝试这样的代码:

plt.cla()
a = [['test', -87, 6, 4, 8]]
df = pd.DataFrame(a, columns=['ssid', 'rssi', 'channel', 'r', 'l'])
x = np.linspace(df['r'], df['l'], 50)
y = int(df['rssi']) * np.sin((x - int(df['channel']) + 2) / 4 * np.pi)
plt.fill(x, y, color='r', alpha=0.3)
plt.show()

像这样打印图表

在此处输入图像描述

不是我想要的。

y.value应该是-990应该frequency height是一样-99-87只有13通道中的高度6

标签: pythonmatplotlibseaborn

解决方案


如果我使用-99(或-100

 y = (-99 - row['rssi']) * ...

 y = -99 - y

然后我翻转值并得到预期的情节

在此处输入图像描述


最小的工作代码。

顺便说一句:对于某些范围(l - r > 4),它需要不同的计算。

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

data = [
    ('ASUS_Kurt',  -76,  2, 0.0,  4.0),
#    ('Javen-2.4G', -91,  3, 1.0,  9.0),   # need different calculations
    ('LIN',        -62,  6, 4.0,  8.0),
    ('wcs2.4G',    -64, 11, 9.0, 13.0),
#    ('LIN-EXT',    -74, 11, 5.0, 13.0),   # need different calculations
    ('test1',      -87,  6, 4.0,  8.0),
    ('test2',      -80, 11, 9.0, 13.0),
]

df = pd.DataFrame(data, columns=['ssid', 'rssi', 'channel', 'r', 'l'])

plt.cla()

all_colors = ['r', 'g', 'b', 'y']

for index, row in df.iterrows():
    x = np.linspace(row['r'], row['l'], 50)
    y = (-99-row['rssi']) * np.sin((x - row['channel'] + 2) / 4 * np.pi)
    y = -99-y
    color = all_colors[ index % len(all_colors) ]
    plt.fill(x, y, color=color, alpha=0.3)
    
plt.show()

编辑:

在此处输入图像描述

Javen-2.4G并且LIN-EXT可能需要不同的计算,因为channel它不在r和之间l

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

data = [
    ('ASUS_Kurt',  -76,  2, 0.0,  4.0),
    ('Javen-2.4G', -91,  3, 1.0,  9.0),   # need different calculations
    ('LIN',        -62,  6, 4.0,  8.0),
    ('wcs2.4G',    -64, 11, 9.0, 13.0),
    ('LIN-EXT',    -74, 11, 5.0, 13.0),   # need different calculations
    ('test1',      -87,  6, 4.0,  8.0),
    ('test2',      -80, 11, 9.0, 13.0),
]

df = pd.DataFrame(data, columns=['ssid', 'rssi', 'channel', 'r', 'l'])

all_colors = ['r', 'g', 'b', 'y']

plt.cla()

plt.xlabel('Wifi Channel')
plt.ylabel('Signal Strength [dBm]')

plt.grid(axis='y', linestyle='--')
plt.grid(axis='x', linestyle=':', color='r', alpha=0.3)

y_major_ticks = np.arange(0, -100, -10)
plt.gca().set_yticks(y_major_ticks)

x_major_ticks = np.arange(0, 15, 1)
plt.gca().set_xticks(x_major_ticks)

plt.ylim(-100, 0)
plt.xlim(-2, 16)

for index, row in df.iterrows():
    diff = (row['l'] - row['r'])
    x = np.linspace(row['r'], row['l'], 50)
    y = (-99-row['rssi']) * np.sin((x - row['r']) / diff * np.pi)
    y = -99-y
    
    color = all_colors[ index % len(all_colors) ]
    
    plt.fill(x, y, color=color, alpha=0.3)
    plt.plot(x, y, color=color)    
    plt.text(row['channel'], row['rssi']+1, row['ssid'], horizontalalignment='center')
    
plt.show()

推荐阅读