1

私はこのようなテキストファイルにたくさんのデータを持っています:

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

誰か助けてもらえますか?

4

2 に答える 2

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()
于 2012-05-18T21:44:28.070 に答える
0

私は次のようなエラーチェックを使用します

return [ result[column] if column < len( result ) else None for result in results]

ファイルに行がないようです。,

于 2012-05-18T19:06:58.373 に答える