我有五个cvs文件,其中包含两个对应于每次运行的样本的Diameter和Intensity的列。每个文件对应于样本的单次运行,这些列的值可能与运行略有不同,但彼此接近。
从csv文件运行的示例:
Diameter,Intensity
3.00e+1,0.00
3.19e+1,0.00
3.39e+1,0.00
3.60e+1,0.00
3.83e+1,0.00
4.08e+1,7.01
我的目标是使用Python绘制一个散点图,读取这五个cvs文件并生成一条散点曲线,将五次运行平均到一条平均曲线中?怎么办?
这是我使用Plotly进行编码的尝试。以下代码的问题在于,当平均五次运行的强度时,曲线绘制不正确:
files = defaultdict(list)
file_start = raw_input("File Starts: ")
def read_data(file):
with open(file,'Ur') as f:
reader=csv.reader(f,delimiter=',')
for row in reader:
if "d (nm)" in row:
continue
else:
files[file].append(row)
os.chdir(".")
for file in glob.glob("*.csv"):
if file_start in file:
read_data(file)
for key, val in files.iteritems():
# R1 is run 1 from the file being read
if "R1" in key:
for v in val:
l1.append(float(v[0]))
l2.append(float(v[1]))
elif "R2" in key:
for v in val:
l3.append(float(v[0]))
l4.append(float(v[1]))
elif "R3" in key:
for v in val:
l5.append(float(v[0]))
l6.append(float(v[1]))
elif "R4" in key:
for v in val:
l7.append(float(v[0]))
l8.append(float(v[1]))
elif "R5" in key:
for v in val:
l9.append(float(v[0]))
l10.append(float(v[1]))
sum_val_0 = 0; avg_l_0 =[]; sum_val_1 = 0; avg_l_1 =[]
for val in zip(l1, l3, l5, l7, l9, l2, l4, l6, l8, l10):
sum_val_0 = sum_val_0 + float(val[0])+float(val[1]) + float(val[2]) +float(val[3])+float(val[4])
avg = sum_val_0/ len(val)
avg_l_0.append(avg)
sum_val_1 = sum_val_1 + float(val[5])+float(val[6]) + float(val[7]) +float(val[8])+float(val[9])
avg_1 = sum_val_1/ 10
avg_l_1.append(avg_1)
trace0 = go.Scatter(
x = avg_l_0,
y= avg_l_1,
line = dict(
color = ('rgb(205, 12, 24)'),
width = 2)
)
data = [trace0]
layout = go.Layout(
dict(showlegend=True,
xaxis = dict(title = 'Diameter', range = [35,1500]),
yaxis = dict(title = 'Intensity', range = [-20,120], showline=True,),
)
)
fig = dict(data=data, layout=layout)