Data Analysis and Scatter Plot different file and different column

241 views Asked by At

i have a lot of files and i want to open, read data1.txt and data2.txt file and then data1.txt file 22. column "x_coordinate" and data2.txt file 23. column "y_coordinate" scatter plot. how can i ?

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()
1

There are 1 answers

5
jgloves On BEST ANSWER

This seems like it would be a lot simpler using a Pandas dataframe. Then, part of your problem is analogous to this question: 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)  

You can perform functions on the data within the dataframe, and you can even make a scatter plot (or other types of plots) very easily.

http://pandas.pydata.org/pandas-docs/version/0.15.0/visualization.html

dataframe.plot(kind-'scatter',x='data1',y='data2')
plt.show()