绘制多传感器数据(Pandas、Matplotlib)

2024-09-27 19:28:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我想首先将我的数据帧按“id”分组,然后按“Datetime”分组。然后根据“日期时间”绘制每个传感器的“温度”数据。谢谢

Sudo代码:

   _, ax = plt.subplots()
    df_ = plots.groupby(['moteid', "Datetime"])
    for sensorID in df_moteid.unique():
      plot df['temp'] against df['Datetime']

我的数据如下所示: enter image description here

编辑: 我通过首先在read_csv中声明index_col='Datetime'来绘制每个senosor数据,使其成为主索引,然后像这样绘制:

_, ax = plt.subplots()
for key, group in df.groupby('moteid'):
    rgb = np.random.rand(3,)
    group.plot.line(ax=ax, y='temp', color=[rgb], label=key)
plt.show()

Tags: 数据keyindffordatetimeplotgroup
1条回答
网友
1楼 · 发布于 2024-09-27 19:28:25

这是显示每个传感器温度随日期时间变化的代码。每个传感器将根据其id具有不同的颜色。我以4个传感器id为例:

import matplotlib.pyplot as plt
colors_moteid = {'1':'red', '2':'blue', '3':'green', '4':'black'}

_, ax = plt.subplots()
for key,group in df_.groupby('moteid'):
    group.plot.scatter(ax=ax, x='Datetime', y='temp', label=key, color = colors_moteid[str(key)])
plt.show()

相关问题 更多 >

    热门问题