Yababaa
Yababaa

Reputation: 577

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.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

So basically (from left to right) date and time, sensor 1, sensor 2, sensor 3, sensor 4, sensor 5. I want to plot this by using Python, Ive read about matplotlib for plotting graphs. But how can I plot this data from a text file? I would like to plot on the x axis the timestamps and on the y axis the data from different sensors in one chart. Im not experienced in matplotlib at all.

For reading the text file I was thinking of something like this:

line = file.readlines()
new_line = line.strip(", ")
date = new_line[0]
sensor1 = new_line[1]
#and so on

Upvotes: 2

Views: 7184

Answers (2)

DrV
DrV

Reputation: 23480

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.

Upvotes: 1

Emanuele Paolini
Emanuele Paolini

Reputation: 10162

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()

Upvotes: 2

Related Questions