1

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()
6
  • 2
    Either your sample data or your code is off, your test data has delimiter space, while the code requires comma, and your code is accessing columns 2 and 3 while the sample data only has 2 columns. Commented Feb 3, 2013 at 8:51
  • Hello...what do you mean its accessing columns 2 and 3?? How do I change that?? Commented Feb 3, 2013 at 9:30
  • I am getting this error: Traceback (most recent call last): File "C:/Python27/jkjk", line 29, in <module> plot(Time, Distance) File "C:\Python27\lib\site-packages\matplotlib\pyplot.py", line 2817, in plot ret = ax.plot(*args, **kwargs) File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 3997, in plot self.add_line(line) File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 1507, in add_line ValueError: could not convert string to float: Commented Feb 3, 2013 at 9:41
  • The column index is zero based, that is, column[0] is the first column, not column[1]. Commented Feb 3, 2013 at 9:41
  • Still not working :O What do u mean that my code might be off? Commented Feb 3, 2013 at 9:43

1 Answer 1

2
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)
Sign up to request clarification or add additional context in comments.

6 Comments

I changed it but it looks weird when I copy pasted it hopefully it makes sense....So basically my first row is nematode no and my first column is no of jumps Then for the next columns I have time and distance for each nematode
Please help me write a code that could plot seperate time and distance graphs for each nematode
Oh no its giving me new error: File "C:/Python27/dsds", line 4, in <module> x, y = np.loadtxt("simple.csv", skiprows=2, usecols=[1, 2], unpack=True) File "C:\Python27\lib\site-packages\numpy\lib\npyio.py", line 803, in loadtxt vals = [vals[i] for i in usecols] IndexError: list index out of range
@Mel19: the code works for the data from the question. Check that simple.csv has at least 3 columns.
@Mel19: Provide actual input data (the first few lines might be enough if all lines have the same number of columns). Provide actual code that you use (exactly 5 lines of code as shown in my answer). Provide actual error/traceback that you get.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.