Reputation: 11
I am very new to python and I only need it so I analyse some data for bio.
Please help on How I can fix the error below,
could not convert string to float
I am trying to plot (Time, Distance)
Here is some sample data:
Nematode No. 1 1 2 2
No. of jumps Time (sec) Distance (mm) Time Distance
1 0.195781141 0.893988392 1.25388 0.56569
2 2.386623538 1.073359412 3.5848484 1.55656
3 2.915538343 1.227371636 4.284848 2.34454545
4 4.993603286 0.653631116 6.4545454 3.65445
5 8.002735854 0.986036142 2.35554 0.2548545
6 10.84267517 0.939671599 4.245454 0.5484848
My code (yet)
from mmap import mmap,ACCESS_READ
from xlrd import open_workbook
from pylab import *
from xlrd import open_workbook,XL_CELL_TEXT
import csv
from scipy import stats
values = csv.reader(open('simple.csv', 'rb'), delimiter=',',skipinitialspace=True)
for column in values:
print column[1],column[2]
Time = column[1]
Distance = column[2]
plot(Time,Distance)
show()
Upvotes: 1
Views: 8258
Reputation: 414235
import numpy as np
import matplotlib.pyplot as plt
x, y = np.loadtxt("simple.csv", skiprows=1, unpack=True)
plt.plot(x, y)
plt.show()
First row contains your column names and can't be converted to floats because they aren't numbers to begin with so you should skip the first row when trying to plot the data.
For the updated data: skip the first 2 rows with headers and plot the 2nd, 3rd columns:
x, y = np.loadtxt("simple.csv", skiprows=2, usecols=[1, 2], unpack=True)
Upvotes: 2