简体   繁体   中英

Handling scientific data files with python for a X-manyY plot. Several .txt's to one .csv

I would like to import data from several .txt files, export them to a single .csv file and finally plot the data with matplotlib (one x - many y's).

Each file contains 2 columns (x and y) with strings in the first row (column names) followed by 100 rows of numbers (decimal and scientific notation). I want to read all files and skip its firstrows. The first columns of the .txt's are the same so I actually need only one of them as the x column of the .csv. Column #2 of each file should be imported in order to create the remaining columns (y1, y2, y3, ...). Also I would like to write new names of the columns as the first row of the .csv.

import numpy as np
import matplotlib.pyplot as plt

data1 = np.genfromtxt('filename1', delimiter=',', skiprows=1)
data2 = np.genfromtxt('filename2', delimiter=',', skiprows=1)
# etc...

x = data1[:,0]
y1 = data1[:,1]
y2 = data2[:,1]
# etc...

plt.figure()
plt.scatter(x, y1)
plt.scatter(x, y2)
# etc...
plt.show()

This should open the files and plot them if you insert your filenames where indicated. Try np.savetxt for writing to a csv file. I'm not sure if this will allow headers though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM