如何从日期时间文件中获取numpy ndarray并绘制与matplotlib的差异?

时间:2015-12-28 18:34:43

标签: python numpy matplotlib

我是一个笨拙的新手,在Linux上使用numpy 1.10.2和Python 2.7.6。我有一个17M日期时间的文件,如"2015-12-24 03:39:02.012"。 我想将差异d[n]-d[n-1]绘制为时间的函数。

从这个文件中获取darray的numpy-ish方法是什么,然后使用matplotlib绘制diff与datetime的方法(如果diff n-1n+1无关紧要?)< / p>

我不需要一个致盲的速度黑客;我宁愿学习惯用的numpy技术。

数据如下:

2015-12-24 03:39:02.009
2015-12-24 03:39:02.012
2015-12-24 03:39:02.015
2015-12-24 03:39:02.018
2015-12-24 03:39:02.021
2015-12-24 03:39:02.024
2015-12-24 03:39:02.027
2015-12-24 03:39:02.030
2015-12-24 03:39:02.033
2015-12-24 03:39:02.036
2015-12-24 03:39:02.039
2015-12-24 03:39:02.042
2015-12-24 03:39:02.045
2015-12-24 03:39:02.048
2015-12-24 03:39:02.051
2015-12-24 03:39:02.054
2015-12-24 03:39:02.057
2015-12-24 03:39:02.060
2015-12-24 03:39:02.063
2015-12-24 03:39:02.066

...... 17M行

所以,要清楚,我想绘制像

这样的东西
datetime64(2015-12-24 03:39:02.009), 3 # second datetime-first datetime
datetime64(2015-12-24 03:39:02.012), 3 # third datetime-second datetime
datetime64(2015-12-24 03:39:02.015), 3 # fourth datetime-third datetime

...

我真正想要的是间隔期间的尖峰以及尖峰发生的时间。

1 个答案:

答案 0 :(得分:1)

Pandas可以在一行中读取文件:

from matplotlib import pyplot as plt
import pandas as pd

df = pd.read_csv('data.txt', header=None, parse_dates=[0], names=['date'])

结果如下:

enter image description here

计算差异

diff = df[1:] - df.shift()[1:]

绘制结果:

plt.plot(df[1:], diff.values)

enter image description here

您可以将值转换为秒:

seconds = diff.date.get_values().astype(float) / 1e9