我在这样的文本文件中有一堆数据:
99150, 2012-05-18 14:30:08.592276
100350, 2012-05-18 14:31:09.093357
97710, 2012-05-18 14:32:09.583485
94980, 2012-05-18 14:33:10.047794
95670, 2012-05-18 14:34:10.559798
97170, 2012-05-18 14:35:11.073576
98850, 2012-05-18 14:36:11.562930
98280, 2012-05-18 14:37:12.058591
97950, 2012-05-18 14:38:12.547585
102510, 2012-05-18 14:39:13.053431
我想制作一个简单的情节并输出图像。我从以下开始:
#!/bin/python
import csv
import matplotlib.pyplot as plt
import numpy as np
filename="pps_counter.log"
def getColumn(filename, column):
results = csv.reader(open(filename), delimiter=",")
return [result[column] for result in results]
time = getColumn(filename,1)
packets = getColumn(filename,0)
plt.figure("Packets Per Minute")
plt.xlabel("Time(minutes)")
plt.ylabel("Number of Packets")
plt.plot(time,packets)
当我运行时,我收到以下错误:
Traceback (most recent call last):
File "plotter.py", line 16, in <module>
time = getColumn(filename,1)
File "plotter.py", line 14, in getColumn
return [result[column] for result in results]
IndexError: list index out of range
有人可以帮忙吗?
答案 0 :(得分:1)
我建议使用csv2rec(或genfromtxt与转换器功能)。这会将时间转换为python日期时间,您可以使用matplotlib
进行绘制。
from matplotlib.mlab import csv2rec
import matplotlib.pyplot as plt
data = csv2rec('pps_counter.log', names=['packets', 'time'])
plt.plot_date(data['time'], data['packets'])
plt.xlabel("Time(minutes)")
plt.ylabel("Number of Packets")
plt.title("Packets Per Minute")
plt.show()
答案 1 :(得分:0)
我会使用一些错误检查 喜欢
return [ result[column] if column < len( result ) else None for result in results]
您的文件中有一行缺少,