首页 > 解决方案 > 当 matplotlib 从 csv 文件中绘制实时数据时,RAM 迅速增加

问题描述

我不是一个严肃的程序员,只是一个想法的粉丝。我创建了这个脚本。它从通过串行连接从真空传感器接收数据的日志文件中绘制最后 60 行的图形,但问题是该脚本正在迅速增加 RAM 填充。它有什么问题,我做错了什么?

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import matplotlib
matplotlib.use('TkAgg')
import csv

fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
plt.subplots_adjust(bottom=0.25)


filename = "loggedData.txt"
last_rows = 60

def update(i):
     file_data = pd.read_csv(filename, skiprows = 2,  delimiter=",",
          header=None, parse_dates=True, usecols=[0, 1])

     time_data=(file_data[0].tail(last_rows))
     vacuum_data=(file_data[1].tail(last_rows))

#     print(xdata)
#     print(ydata)

#     ax1.clear()
     plt.cla()
     ax1.plot(time_data,vacuum_data)
     fig.autofmt_xdate(rotation=45)
     ax1.set_xlabel('Measurement time')
     ax1.set_ylabel('Vacuum (Torr)')
     plt.yscale('log')
     ax1.grid(which='major', color='#CCCCCC', linestyle='--')
     ax1.grid(which='minor', color='#CCCCCC', linestyle=':')


ani = animation.FuncAnimation(fig, update, interval=1000)

plt.show()

标签: pythonmatplotlib

解决方案


推荐阅读