Python read csv data

From wikiluntti

Introduction

Read csv data into python

Numpy 1

The simplest method?

import numpy as np

data = np.genfromtxt('filename_gps.csv', delimiter=",")

Numpy

Datafile: File:Co2 data1.csv

import csv
import matplotlib.pyplot as plt
import numpy as np

datalist = []
with open('co2_data1.csv', mode ='r')as file:
  csvFile = csv.reader(file)
  for line in csvFile:
        t = float( line[0])
        T = float( line[1])
        h = float( line[2])
        CO2 = float( line[3])
        datalist.append(  [t, T, h, CO2] )
data =  np.array( datalist)

Plot the data

# Muuta sekunnit tunneiksi
data[:,0] = data[:,0]/60/60 + 16

# Tulostus
#
ax1 = plt.subplot(311)
ax1.plot(data[:,0], data[:,1])
ax1.set_xlabel('Aika [s]')
ax1.set_ylabel('Lampot')
#Piirrä tunnin alku ja loppuaika
ax1.plot( [ 16.02, 16.02], [10, 30], 'r' )
ax1.plot( [ 16.12, 16.12], [10, 30], 'r' )
ax1.text(16.05, 25, "Matikka", fontsize=12)
ax1.set_ylim([20, 30])

ax2 = plt.subplot(312)
ax2.plot(data[:,0], data[:,2])
ax2.set_xlabel('Aika [s]')
ax2.set_ylabel('Ilmankosteus')

ax3 = plt.subplot(313)
ax3.plot(data[:,0], data[:,3])
ax3.set_xlabel('Aika [s]')
ax3.set_ylabel('CO2')

plt.show()  # display

More fun