我有一个可以读取和运行多个文件并从每个文件生成多个图形的代码。我想分别保存所有数字。但是我只能从每个文件中保存一个数字。例如,我有17个文件,每个文件生成3个数字,所以我总共应该有51个数字。该代码显示所有51个图形(每个文件3个),但每个文件仅保存一个图形,共保存17个图形。如何分别保存所有数字?
import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
for file in os.listdir(r'/mydir'):
if file.endswith(".txt"):
print(os.path.join("/mydir",file))
life_time= pd.read_csv(file,sep = "\t")
life_time.columns = ["Time","Counts"]
time= life_time.Time[2:599]
time_1=time-1e-06
x1=time_1.reset_index(drop=True)
bin1 = life_time.Counts[2:599]
bin2 = life_time.Counts[602:1199]
bin3 = life_time.Counts[1202:1799]
"""Reser the index"""
r_bin1=bin1.reset_index(drop=True)
r_bin2=bin2.reset_index(drop=True)
r_bin3=bin3.reset_index(drop=True)
x= x1[1:598]
y1=r_bin1[1:598]
y2=r_bin2[1:598]
y3=r_bin3[1:598]
#From each dataset I have created three y value such as y1,y2,y3 and then created a list
y = [y1,y3]
def exponenial_func(x,a,b,c):
return a*np.exp(-b*x)+c
for i in y:
popt,pcov = curve_fit(exponenial_func,x,i)
a,c = popt
yy = exponenial_func(x,*popt)
plt.plot(x,i)
plt.plot(x,yy,'r--')
plt.savefig("file{}.jpg".format(file),dpi = 600,bbox_inches = "tight")
plt.show()
例如,我的文件名为USA_1,USA_2 ......等。我要保存图形名称为USA_1_bin1,USA_1_bin2,USA_1_bin3,USA_2_bin1,USA_2_bin2,USA_2_bin3 ....的图形。...