gpt4 book ai didi

Python 根据 csv 文件绘制时间与数据包的关系

转载 作者:太空宇宙 更新时间:2023-11-03 19:18:13 24 4
gpt4 key购买 nike

我在文本文件中有一堆数据,如下所示:

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

有人可以帮忙吗?

最佳答案

我建议使用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()

关于Python 根据 csv 文件绘制时间与数据包的关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10658271/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com