1

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()
pnuts
  • 58,317
  • 11
  • 87
  • 139
np.meta
  • 13
  • 3

1 Answers1

0

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()   
Community
  • 1
  • 1
jgloves
  • 719
  • 4
  • 14
  • TypeError: Empty 'DataFrame': no numeric data to plot what is [:-4] mean?How do i specify the columns i want? my codes are true? – np.meta Jun 17 '15 at 19:02
  • Could you post the output of print(dataframe.head())? – jgloves Jun 17 '15 at 19:06
  • My example code assumes only one value per line. Are there multiple columns in your files? If so, the answer will be different, and I can update it. The [:-4] slices the .txt extension from your file paths to name the dataframe columns after the files. – jgloves Jun 17 '15 at 19:11
  • Could you add the first few lines of one of your files to your question, so I can see the format? – jgloves Jun 17 '15 at 19:14
  • yes i have multiple columns. and i want to select [15], [16], [17] columns only one file, after i select [15], [16], [17] columns of other file. then i want to scatter plot with first file [15] and second file [16] or both of [15]. actually i want this plottig :https://imageshack.com/i/p9RKqn65p – np.meta Jun 17 '15 at 19:28