Plot Cansat or any csv data using Python: Difference between revisions

From wikiluntti
(Created page with "== Introduction == Many different methods exists. Here, we describe some. == Theory == == Method 1: CSV reader == Robust for errors in the csv file. <syntaxhighlight lang="python"> </syntaxhighlight> == Method 2: Numpy == Do not care about the trailing spaces. <syntaxhighlight lang="python"> </syntaxhighlight> == Method 3: Pandas == Problems with trailing spaces. <syntaxhighlight lang="python"> </syntaxhighlight>")
 
Line 10: Line 10:


<syntaxhighlight lang="python">
<syntaxhighlight lang="python">
import csv
import matplotlib.pyplot as plt
#Create the array to store the datA
results = []
with open('satelliitti2021.log') as csv_file:
    csv_read=csv.reader(csv_file, delimiter=" ")
    for row in csv_read:
        results.append(row)
#choose the data:
col = 6
data = []
for row in results:
    data.append( float( row[col] ) )
fig, ax = plt.subplots(figsize=(12,6))
print( data )
plt.plot( data )
plt.xlim([0,1600])
plt.ylim([-1, 5])
plt.show()
</syntaxhighlight>
</syntaxhighlight>



Revision as of 22:21, 9 February 2023

Introduction

Many different methods exists. Here, we describe some.

Theory

Method 1: CSV reader

Robust for errors in the csv file.

import csv
import matplotlib.pyplot as plt

#Create the array to store the datA
results = []

with open('satelliitti2021.log') as csv_file:
    csv_read=csv.reader(csv_file, delimiter=" ")
    for row in csv_read:
        results.append(row)


#choose the data:
col = 6
data = []
for row in results:
    data.append( float( row[col] ) )

fig, ax = plt.subplots(figsize=(12,6))
print( data )
plt.plot( data )
plt.xlim([0,1600])
plt.ylim([-1, 5])
plt.show()

Method 2: Numpy

Do not care about the trailing spaces.

Method 3: Pandas

Problems with trailing spaces.