我有很多文件,我想打开,读取data1.txt和data2.txt文件,然后读取data1.txt文件22.列“x_coordinate”和data2.txt文件23.列“y_coordinate”散点图。我怎么样?
with open('data1.txt') as f:
with open('data2.txt') as f2:
data1 = f.readlines()
data2 = f2.readlines()
f1.xArr=[]
f1.yArr=[]
f1.zArr=[]
f2.xArr2=[]
f2.yArr2=[]
f2.zArr2=[]
f.lc=0
f.bx=0.0
f.pl=0.0
f.xA=0.0
f.yA=0.0
f.zA=0.0
f2.lc2=0
f2.bx2=0.0
f2.pl2=0.0
f2.xA2=0.0
f2.yA2=0.0
f2.zA2=0.0
while(data1 !=''):
f.lc=f.lc+1
if f.lc > 10000:
break
f.xy=list(data[0])
f.bx=int(f.xy[5])
f.xA=float(f.xy[22])
f.yA=float(f.xy[23])
f.zA=float(f.xy[24])
if (Name_c=='textfile1'):
s1=[25, 10, -19, 19]
xArr.append(f.xA)
yArr.append(f.yA)
zArr.append(f.zA)
data=f.readline()
答案 0 :(得分:0)
使用Pandas数据帧似乎更简单。 然后,你的部分问题类似于这个问题: Read multiple *.txt files into Pandas Dataframe with filename as column header
import pandas as pd
import matplotlib.pyplot as plt
filelist = ['data1.txt', 'data2.txt']
dataframe = pd.concat([pd.read_csv(file, names=[file[:-4]]) for file in filelist], axis=1)
您可以对数据框内的数据执行功能,甚至可以非常轻松地制作散点图(或其他类型的图)。
http://pandas.pydata.org/pandas-docs/version/0.15.0/visualization.html
dataframe.plot(kind-'scatter',x='data1',y='data2')
plt.show()