【问题标题】:Python line plot multiple time series on same plotPython线在同一个图上绘制多个时间序列
【发布时间】:2018-03-04 20:28:58
【问题描述】:

我正在解析一个文件,该文件包含多个时间序列的时间序列时间戳数据,我想在 python 中对其进行解析,然后使用 matplotlib 为每组时间序列数据创建一个带有独立线的单线图。我正在解析的数据如下所示:

time label   value
1.05 seriesA 3.925
1.09 seriesC 0.245
2.13 seriesB 12.32
2.73 seriesC 4.921

我已将该文件解析为一个列表字典,其中包含以系列标签为键的 (time,value) 元组。我正在努力解决如何从这个到包含所有这些数据的单线图。我想要在一个情节上为 seriesA、seriesB、seriesC 等提供独立的线条。有什么指点吗?

编辑:根据要求,字典如下。我很难找出存储这些数据的最佳方式,所以我使用的数据结构可能也是一个问题。下面的键是不同的时间序列标签,值是 (time,value) 元组的列表。无论如何,这里是:

{'client1': [(861.991698574, 298189000.0), (862.000768158, 0.0)], 
'client2': [(861.781502324, 0.0), (861.78903722, 153600000.0), 
(862.281483262, 0.0), (862.289038158, 153600000.0)], 'client3': 
[(862.004470762, 3295674368.0), (862.004563939, 3295674368.0), 
(862.03981821, 799014912.0), (862.040403314, 1599078400.0), 
(862.540269616, 3295674368.0), (862.55133097, 1599078400.0)]}

【问题讨论】:

  • 这种表格数据确实是pandas的用例。查看DataFrame.groupy。如果你想在 python 中做,你可以使用itertools.groupby 按标签分组,然后用 matplotlib 绘图。

标签: python matplotlib


【解决方案1】:

我喜欢 pandas 来解决这类问题。

首先,将数据放入pandas数据框:

import pandas as pd

data = {'client1': [(861.991698574, 298189000.0), (862.000768158, 0.0)], 
'client2': [(861.781502324, 0.0), (861.78903722, 153600000.0), 
(862.281483262, 0.0), (862.289038158, 153600000.0)], 'client3': 
[(862.004470762, 3295674368.0), (862.004563939, 3295674368.0), 
(862.03981821, 799014912.0), (862.040403314, 1599078400.0), 
(862.540269616, 3295674368.0), (862.55133097, 1599078400.0)]}

time = []
label = []
value = []

for k, v in data.items():
    for tup in v:
        label.append(k)
        time.append(tup[0])
        value.append(tup[1])

df = pd.DataFrame({'time':time, 'label':label, 'value':value})

导致这个数据框:

>>> df
      label        time         value
0   client1  861.991699  2.981890e+08
1   client1  862.000768  0.000000e+00
2   client2  861.781502  0.000000e+00
3   client2  861.789037  1.536000e+08
4   client2  862.281483  0.000000e+00
5   client2  862.289038  1.536000e+08
6   client3  862.004471  3.295674e+09
7   client3  862.004564  3.295674e+09
8   client3  862.039818  7.990149e+08
9   client3  862.040403  1.599078e+09
10  client3  862.540270  3.295674e+09
11  client3  862.551331  1.599078e+09

然后,您可以这样做:

by_label = df.groupby('label')

for name, group in by_label:
    plt.plot(group['time'], group['value'], label=name)

plt.legend()
plt.show

关于如何将数据存储在字典中;有不同的方法可以解决这个问题,但如果我是你,并且能够通过 pandas 轻松使用你的数据,我会使用以下形式的字典:

data = {'label':['client1', 'client1', 'client2', ...], 
 'time':[time1, time2, time3, ...], 
 'value':[value1, value2, value3, ...]}

确保您的所有列表都以正确的方式排序(所有 3 个键的索引 0 是数据帧的第 0 行,索引 1 是第 1 行,等等)。然后要导入 pandas,你需要做的就是df = pd.DataFrame(data)

【讨论】:

  • 'from matplotlib import pyplot as plt' 需要此代码才能工作,考虑编辑
  • 这很好,但由于某种原因我遇到了这个问题:ValueError: setting an array element with a sequence.
【解决方案2】:

简答:

高亮并ctrl+c下面的数据:

label        time         value
client1  861.991699  2.981890e+08
client1  862.000768  0.000000e+00
client2  861.781502  0.000000e+00
client2  861.789037  1.536000e+08
client2  862.281483  0.000000e+00
client2  862.289038  1.536000e+08
client3  862.004471  3.295674e+09
client3  862.004564  3.295674e+09
client3  862.039818  7.990149e+08
client3  862.040403  1.599078e+09
client3  862.540270  3.295674e+09
client3  862.551331  1.599078e+09

然后运行这个 sn-p:

# imports
import pandas as pd

# read data from the clipboard
df = pd.read_clipboard(sep='\\s+')

# reshape the data to get values by time for each label
df = df.pivot(index='time', columns='label', values='value')

# Replace nans by forward filling existing values
df = df.fillna(method = 'ffill')

# You'll still have to handle the missing values in the beginning of the coloumns
df = df.fillna(method = 'bfill')

# A simple plot:
df.plot()

然后你会得到:


详情

这个问题有一些令人困惑的元素。如果您的来源数据如您所说,采用以下形式:

time label   value
1.05 seriesA 3.925
1.09 seriesC 0.245
2.13 seriesB 12.32
2.73 seriesC 4.921

但您数据的真正内容是:

{'client1': [(861.991698574, 298189000.0), (862.000768158, 0.0)], 
'client2': [(861.781502324, 0.0), (861.78903722, 153600000.0), 
(862.281483262, 0.0), (862.289038158, 153600000.0)], 'client3': 
[(862.004470762, 3295674368.0), (862.004563939, 3295674368.0), 
(862.03981821, 799014912.0), (862.040403314, 1599078400.0), 
(862.540269616, 3295674368.0), (862.55133097, 1599078400.0)]}

那么你的数据真正的内容和形式应该是:

label        time         value
client1  861.991699  2.981890e+08
client1  862.000768  0.000000e+00
client2  861.781502  0.000000e+00
client2  861.789037  1.536000e+08
client2  862.281483  0.000000e+00
client2  862.289038  1.536000e+08
client3  862.004471  3.295674e+09
client3  862.004564  3.295674e+09
client3  862.039818  7.990149e+08
client3  862.040403  1.599078e+09
client3  862.540270  3.295674e+09
client3  862.551331  1.599078e+09

无论如何,绝对没有理由使用字典来获取您的

[...]包含所有这些数据的单线图。我想在一个情节上为 seriesA、seriesB、seriesC 等提供独立的线条。

我相信最有效的方法是来自pandas docs 的Reshaping and Pivot Tables。从那里您可以使用df.plot() 直接绘制数据。

高亮并ctrl+c上面的数据,你就可以开始了:

# imports
import pandas as pd

# read data from the clipboard
df = pd.read_clipboard(sep='\\s+')

# reshape the data to get values by time for each label
df = df.pivot(index='time', columns='label', values='value')
print(df)

这应该代表您所需的数据形式:

label           client1      client2       client3
time                                              
861.781502          NaN          0.0           NaN
861.789037          NaN  153600000.0           NaN
861.991699  298189000.0          NaN           NaN
862.000768          0.0          NaN           NaN
862.004471          NaN          NaN  3.295674e+09
862.004564          NaN          NaN  3.295674e+09
862.039818          NaN          NaN  7.990149e+08
862.040403          NaN          NaN  1.599078e+09
862.281483          NaN          0.0           NaN
862.289038          NaN  153600000.0           NaN
862.540270          NaN          NaN  3.295674e+09
862.551331          NaN          NaN  1.599078e+09

鉴于时间索引有些特殊,仍有一些问题需要处理。为了使这个数据绘图友好,我们应该处理缺失值。这很容易在下一个 sn-p 中使用来自pandas docs 的df.fillna 完成:

# Replace nans by forward filling existing values
df = df.fillna(method = 'ffill')

# You'll still have to handle the missing values
# in the beginning of the coloumns
df = df.fillna(method = 'bfill')

现在您只需使用df.plot()即可获得折线图:

编辑:

让我知道您的数据源是什么,以便为您提供一些有关如何读取和存储数据的提示。同样,熊猫和最有可能是要走的路。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-24
    • 1970-01-01
    • 2019-12-02
    • 1970-01-01
    • 2019-05-04
    • 2014-07-20
    • 2022-01-13
    • 2017-05-09
    相关资源
    最近更新 更多