I have a csv file which contains four columns. The first column in time, the second, third and fourth columns are Accelerometer readings. I want to plot Time on X-Axis and the Accelerometer reading on Y-Axis.
Sample Data:
0 1.0969 9.7721 0.614
20 1.1146 9.7501 0.7444
40 1.1146 9.7501 0.7444
60 1.0124 9.7151 0.7169
79 1.0124 9.7151 0.7169
100 1.0927 9.7324 0.7356
120 1.0927 9.7324 0.7356
Here is what I have so far.
from numpy import genfromtxt
import csv
import matplotlib.pyplot as plt
#import numpy as np
# Open the desired file for reading
f = open('walk-shoe.csv', "rb")
# create a object of csv class and read the file
# use ',' as a delimiter
reader = csv.reader(f, delimiter=',')
time_row = 0
accel_1_row = 0
accel_2_row = 0
accel_3_row = 0
time = []
accel_1 = []
accel_2 = []
accel_3 = []
# create a list of 'Time in ms'
for row in reader:
# Skip the first row
time_row = time_row + 1
if time_row == 1:
continue
time.append(row[0])
accel_1.append(row[1])
accel_2.append(row[2])
accel_3.append(row[3])
# print the contents of the list
# print time
#print accel_1
#print accel_2
#print accel_3
# append all the list accelerometer list together
final_accel = []
final_accel.append(accel_1)
final_accel.append(accel_2)
final_accel.append(accel_3)
#print final_accel
# plot the graph
for i in range(len(final_accel)):
plt.plot(time,[pt[i] for pt in final_accel],label = 'id %s'%i)
plt.legend()
plt.show()
I want to plot all the sensor readings on one graph on y axis and time in x axis
You seem to be importing numpy in the code you give, therefore I will take that to mean that library is available to you. Numpy lets you read in data very easily using numpy.loadtxt().
You can then create a for loop which goes through columns 1 to 3 and plots data against column 0 (time).
import numpy as np
import matplotlib.pyplot as plt
data = np.loadtxt('walk-shoe.csv', delimiter=',', dtype=float)
print (data)
#[[ 0. 1.0969 9.7721 0.614 ]
# [ 20. 1.1146 9.7501 0.7444]
# [ 40. 1.1146 9.7501 0.7444]
# [ 60. 1.0124 9.7151 0.7169]
# [ 79. 1.0124 9.7151 0.7169]
# [ 100. 1.0927 9.7324 0.7356]
# [ 120. 1.0927 9.7324 0.7356]]
for i in range(1,data.shape[1]):
plt.plot(data[:,0], data[:,i], label='id %s' %i)
plt.legend()
plt.show()

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With