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

From wikiluntti
Line 62: Line 62:
import matplotlib.pyplot as plt
import matplotlib.pyplot as plt


df = pd.read_csv('satelliitti2021.log',
df = pd.read_csv('satelliitti2021.txt',
#df = pd.read_csv('satelliitti_edited.log',
                   sep=" ",
                   sep=" ",
                   header = None,
                   header = None,

Revision as of 22:30, 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.txt') 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.

import matplotlib.pyplot as plt
import numpy as np

results = np.genfromtxt('satelliitti2021.txt', delimiter=' ')

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

Method 3: Pandas

Problems with trailing spaces.

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv('satelliitti2021.txt',
                   sep=" ",
                   header = None,
                   names=["v1", "v2", "v3", "ax", "ay", "az"])
#data.columns = ["time", "v1", "v2", "v3", "ax", "ay", "az"]
df.index.name="foo"
print( df )

print( df.columns )
print(df.v1)

ax = df.plot()
ax.set_ylim(-1,5)
plt.show()