2

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
1
  • gnuplot could do this directly. From inside gnuplot: set timefmt "%Y-%m-%d %H:%M:%S"; set xdata time;plot 't.gp' using 1:2 Commented Jul 17, 2014 at 15:43

2 Answers 2

2

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

Comments

1

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.

Comments

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.