Skip to content Skip to sidebar Skip to footer

How To Plot Data With Python From Text File

I have a text file with almost 50k lines of data from sensors that are connected to a raspberry pi. It looks something like this: 2014-07-16 15:57:35.536579, 128, 251, 254, 255, 30

Solution 1:

I suggest to use pandas (which is something similar to R). Supppose your input sample is in file 'data.csv':

import pandas as pd
df = pd.read_csv('data.csv', parse_dates=True,index_col=0,
        names = ['timestamp','x','y','z','w','k'])
df.plot()

Solution 2:

If you do not want to install pandas, the "pure NumPy" solution is to use `

import numpy as np
import datetime

# date field conversion function
dateconv = lambda s: datetime.strptime(s, '%Y-%M-%D %H:%M:%S:.%f')

col_names = ["Timestamp", "val1", "val2", "val3", "val4", "val5"]
dtypes = ["object", "uint8", "uint8", "uint8", "uint8", "float"]
mydata = np.genfromtxt("myfile.csv", delimiter=',', names=col_names, dtype=dtypes, converters={"Time": dateconv})

After this the contents of mydata:

array([('2014-07-16 15:57:35.536579', 128, 251, 254, 255, 30.062),
       ('2014-07-16 15:57:37.763030', 132, 252, 250, 255, 30.062),
       ('2014-07-16 15:57:39.993090', 135, 249, 239, 255, 30.125),
       ('2014-07-16 15:57:42.224499', 142, 251, 221, 255, 30.125),
       ('2014-07-16 15:57:44.452908', 152, 252, 199, 255, 30.187),
       ('2014-07-16 15:57:46.683009', 162, 246, 189, 255, 30.187)], 
      dtype=[('Timestamp', 'O'), ('val1', 'u1'), ('val2', 'u1'), ('val3', 'u1'), ('val4', 'u1'), ('val5', '<f8')])

And now you can try, e.g., mydata['val5']:

array([ 30.062,  30.062,  30.125,  30.125,  30.187,  30.187])

The datetime.datetime objects are now stored as objects. Everything else is stored with the datatype you have specified.

Post a Comment for "How To Plot Data With Python From Text File"