我是初学者python编码器。
我正在处理以下代码,以绘制较长的数据列表。我要处理的文件数据约为500MB。每次完成执行后,Spyder的速度都会变慢,而我最终经常重新启动内核。为什么会这样?
import numpy as np
import matplotlib.pyplot as plt
import time
with open('morelines004.dat') as data_f:
#creo lista str con elementi-linee separate da line boundary
lines = data_f.read().splitlines()
print("--- %s seconds splitlines ok ---" % (time.time() - start_time))
#elimino elementi lista vuoti
lines = [line.split(' ') for line in lines if line!='']
print("--- %s seconds split ok ---" % (time.time() - start_time))
data=[]
#creo lista dati eliminando header
for line in lines:
data.append(line[4:])
datach1=[]
datach3=[]
datach7=[]
for i in range(1,len(data)+1,17):
datach1.append(data[i]) #plug
datach3.append(data[i+2]) #g-2
datach7.append(data[i+6]) #trigger
print("--- %s seconds split data ok ---" % (time.time() - start_time))
#converte data in un numpy array poi di float
datach1=np.array(datach1).astype(np.float)
datach3=np.array(datach3).astype(np.float)
datach7=np.array(datach7).astype(np.float)
### G-2 Event Plotter
for event in range(0,1001,20):
plt.figure(event,figsize=(10,10))
plt.plot(datach3[event][1:])
plt.plot(datach1[event][1:],c='red')
plt.plot(datach7[event][1:],c='darkgray')
plt.xlabel('ns')
plt.ylabel('ADC')
plt.title('g-2. Event: %i' %event )
plt.savefig('img/004g-2samples/004g-2event%i.png' %event)
plt.show()
plt.close()