我有一个包含日期的定宽数据文件,但是当我尝试绘制数据时,日期在x轴上显示不正确。
我的文件长这样
2014-07-10 11:49:14.377102 45
2014-07-10 11:50:14.449150 45
2014-07-10 11:51:14.521168 21
2014-07-10 11:52:14.574241 8
2014-07-10 11:53:14.646137 11
2014-07-10 11:54:14.717688 14
等等
我使用pandas来读取文件
#! /usr/bin/env python
import pandas as pd
import matplotlib.pyplot as plt
data = pd.read_fwf('myfile.log',header=None,names=['time','amount'],widths=[27,5])
data.time = pd.to_datetime(data['time'], format='%Y-%m-%d %H:%M:%S.%f')
plt.plot(data.time,data.amount)
plt.show()
那么我想问题在于从pandas转换为matplotlib datetime,应该如何进行转换?
我也尝试直接使用pandas:
data.time = pd.to_datetime(data['time'], format='%Y-%m-%d %H:%M:%S.%f')
data.set_index('time') # Fails!!
data.time.plot()
但是这会失败,出现以下错误:
TypeError: 空的'Series':没有数值数据可绘制
(Tips: The original text is in English, so I translated it to Chinese according to the requirement. The translated text may not be accurate or grammatically correct.)
import matplotlib.pyplot as plt
import pandas as pd
- Gian Luca Scoccia