Python read csv data: Difference between revisions
From wikiluntti
(→Numpy) |
|||
(4 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
== Introduction == | == Introduction == | ||
Read csv data into python | Read csv data into python | ||
== Numpy 1 == | |||
The simplest method? | |||
<syntaxhighlight lang="python"> | |||
import numpy as np | |||
data = np.genfromtxt('filename_gps.csv', delimiter=",") | |||
</syntaxhighlight> | |||
== Numpy == | == Numpy == | ||
Datafile: [[File:Co2_data1.csv|Co2_data1.csv]] | |||
[[File:Read csv.png|thumb]] | |||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
Line 23: | Line 37: | ||
== Plot the data == | == Plot the data == | ||
<syntaxhighlight lang="python"> | |||
# 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 | |||
</syntaxhighlight> | |||
== More fun == |
Latest revision as of 00:16, 30 March 2025
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