python - 如何从日期时间文件中获取 numpy ndarray 并使用 matplotlib 绘制差异?

标签 python numpy matplotlib

我是一个 numpy 新手,在 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 n-1 无关紧要) n+1)?

我不需要炫目的速度技巧;我宁愿学习惯用的 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

... 1700 万行

所以,要明确一点,我想绘制类似的东西

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

...

我真正要寻找的是间隔中的尖峰以及尖峰发生的时间。

最佳答案

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

关于python - 如何从日期时间文件中获取 numpy ndarray 并使用 matplotlib 绘制差异?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34498828/

相关文章:

Python 3 项目间导入不起作用

python - 在 python opencv 中从其值(欧氏距离)检索图像

python - 包含列表的字典列表

python - 使用 scipy.io.loadmat 从 .mat Matlab 文件将字典键转换为在 Python 中具有相同值的变量名

python - 迭代 numpy 数组以查找子数组中留下行索引的最大值

python - 如何使线图中的颜色在 matplotlib 和 python 中淡出

python根据多数更改字典的值

python - 如何编写 Python 正则表达式来获取 btrfs subvol ID

python - 使 plt.colorbar 扩展到 vmin/vmax 前后的步骤

python - 如何在Python中使用Matplotlib绘制直方图并获取概率?