[Tutor] Reading in large .dat file
Colin Ross
colin.ross.dal at gmail.com
Wed Jun 29 13:05:47 EDT 2016
Good afternoon,
I have a .dat file that is 214 rows by 65 columns that I would like to read
into python and plot the 1st column versus all other columns.
My code:
#########################################################################
import numpy as np
import scipy
import pylab as pl
import matplotlib
from matplotlib.ticker import ScalarFormatter, FormatStrFormatter
import sys
# Load in text from .dat file
sed = np.loadtxt('spectra.dat', unpack = True)
# Access column data
col1 = sed[0]
col2 = sed[1]
col3 = sed[2]
col4 = sed[3]
col5 = sed[4]
wavelength = col1
flux_1 = col2
flux_2 = col3
flux_3 = col4
flux_4 = col5
pl.plot(wavelength,flux_1)
pl.plot(wavelength,flux_2)
pl.plot(wavelength,flux_3)
pl.plot(wavelength,flux_4)
pl.xscale('log')
pl.show()
#########################################################################
This is fine if I want to write out a separate line for each of the 65
columns, but I would like to simplify the code by looping over the data.
Can someone please help me formatting the loop correctly?
Thank you.
Colin
More information about the Tutor
mailing list