Reputation: 158
import csv
import matplotlib.pyplot as plt
filename = r"C:\Users\name\Desktop\sampledata.csv"
with open(filename, newline='') as file:
reader = csv.reader(file)
header = next(reader)
data = []
for row in reader:
measured_time = float(row[0])
wv1 = float(row[1])
wv2 = float(row[2])
data.append([measured_time, wv1,wv2])
time=data[0]
y1=data[1]
plt.plot(time,y1)
plt.show()
Works just fine, but as this was a test before I work with the real data.
Now the real data doesn't have a header and consists on only 2 columns.
import csv
import matplotlib.pyplot as plt
filename = r"C:\Users\name\Desktop\real_data.csv"
with open(filename, newline='') as file:
reader = csv.reader(file,delimiter=';')
data = []
for row in reader:
measured_time = float(row[0])
wv1 = float(row[1])
data.append([measured_time, wv1])
time=data[0]
y1=data[1]
plt.plot(time,y1)
plt.show()
this yields
time=[100,1]
y1=[110,2]
so basically just the first 2 rows while the first codeblock correctly extracts the columns
time=[100 110 ...]
y1=[1 2 ...]
The same thing happens when I use
import csv
import matplotlib.pyplot as plt
filename = r"C:\Users\name\Desktop\sampledata.csv"
with open(filename, newline='') as file:
reader = csv.reader(file)
header = next(reader)
data = []
for row in reader:
measured_time = float(row[0])
wv1 = float(row[1])
data.append([measured_time, wv1])
time=data[0]
y1=data[1]
plt.plot(time,y1)
plt.show()
For now my workaround is
data = np.array(data).astype("float")
time=data[:,0]
y1=data[:,1]
Surely there has to be a better way, I'm just starting with Python.
Data used:
sample data
Time,279 nm,306 nm
0,100,150
1,110,175
2,125,230
3,130,245
real data
3.986931e+002;0.000000e+000
4.021733e+002;0.000000e+000
Upvotes: 0
Views: 135
Reputation: 5286
list(zip())
allows to traspose a set of sequences:
time = [100, 1]
y1 = [110, 2]
l = list(zip(time, y1))
print(l) # [(100, 110), (1, 2)]
time = l[0] # (100, 110)
y1 = l[1] # (1, 2)
They are tuples that you can use as lists or you can transform them to lists:
time = list(l[0]) # [100, 110]
y1 = list(l[1]) # [1, 2]
If you want to get them straight into the opposite form you could swap the reading part:
import csv
import matplotlib.pyplot as plt
filename = r"C:\Users\name\Desktop\sampledata.csv"
with open(filename, newline='') as file:
reader = csv.reader(file)
# header = next(reader)
time = []
y1 = []
for row in reader:
time.append( float(row[0]) )
y1.append( float(row[1]) )
plt.plot(time,y1)
plt.show()
Upvotes: 1