简答:
高亮并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()即可获得折线图:
编辑:
让我知道您的数据源是什么,以便为您提供一些有关如何读取和存储数据的提示。同样,熊猫和最有可能是要走的路。