Pyplot只显示绘图图的最后60秒



我对matplotlib和python是个新手。

  • 我在windows上运行typeperf.exe,基本上可以使用cli实时读取处理器信息:

C:>typeperf "Procesador(_Total)% de tiempo de procesador" > C:UsershbDocumentsScriptslog.csv

  • CSV的数据结构如下:

"07/18/2018 15:50:55.574","3.429826" "07/18/2018 15:50:56.577","0.307183" "07/18/2018 15:50:57.589","18.932128"

  • 在同一时间-同一台机器-我运行了一个代码,基本上读取CSV生成实时图(它有效!(

  • 每1秒绘制一条新线。

目标:使图形仅显示捕获的最后60秒,并继续使用CSV中的新数据。

我读过pyplot文档,尝试过.clf((和.clear((,但没有成功。以下是我遇到困难的代码部分:

def animate(i):
with open(data_in, 'r', newline='') as f_input:
x, y = [], []
end_t = time.time() + 60
while time.time() < end_t:
for line in range(2):
next(f_input)
for row in csv.reader(f_input):
if row:
x.append(datetime.strptime(row[0], '%m/%d/%Y %H:%M:%S.%f'))
y.append(float(row[1]))
ax1.clear()
plt.title('systemn')
plt.xlabel('Current Time')
plt.ylabel('Current HTTP Connectionsn')
ax1.plot(x,y)
time_format = DateFormatter('%H:%M:%S')
plt.gca().xaxis.set_major_formatter(time_format)
start += 1
data_out = animation.FuncAnimation(fig, animate, interval=1000)
plt.show()

我尝试了while循环,试图让情节只读那么长时间。它不起作用。

你能给我指一指正确的路吗?

谢谢,

X和Y当前包含.csv文件中的所有时间步长和值。您不能完全绘制X和Y,只能绘制最后60个值:

if len(x) < 60:
ax1.plot(x,y)
else:
ax1.plot(x[60:],y[60:])

随着.csv文件随着时间的推移而增长,这种方法可能会变得很麻烦。在这种情况下,不时刷新(和归档(文件应该会修复它

最新更新