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>") |
|||
(25 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
== Introduction == | == Introduction == | ||
Many different methods exists. Here, we describe some. | Many different methods exists. Here, we describe some. This use | ||
[[https://wiki.luntti.net/images/1/1e/Satelliitti2021.txt]] data file. Download that and test the codes. | |||
== Theory == | == Theory of plotting log data == | ||
== Method 1: CSV reader == | === Method 1: CSV reader === | ||
Robust for errors in the csv file. | Robust for errors in the csv file. | ||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
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() | |||
</syntaxhighlight> | </syntaxhighlight> | ||
== Method 2: Numpy == | === Method 2: Numpy === | ||
Does not care about the trailing spaces. | |||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
import matplotlib.pyplot as plt | |||
import numpy as np | |||
results = np.genfromtxt('satelliitti2021.txt', delimiter=' ') | |||
fig, ax = plt.subplots(figsize=(12,6)) | |||
plt.plot( results[:,4] ) | |||
plt.xlim([0,1600]) | |||
plt.ylim([-1, 5]) | |||
plt.show() | |||
</syntaxhighlight> | </syntaxhighlight> | ||
== Method 3: Pandas == | === Method 3: Pandas === | ||
Problems with trailing spaces. | Problems with trailing spaces. | ||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
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() | |||
</syntaxhighlight> | </syntaxhighlight> | ||
== Real time plotting == | |||
How to use Python to plot data acquired from the usb (COM) port. Will plot it real time, and save the data to be analyzed later. | |||
=== Method 1 on Windows === | |||
<syntaxhighlight lang="python"> | |||
import csv | |||
import matplotlib.pyplot as plt | |||
#Create the array to store the datA | |||
results = [] | |||
with open(r'C:\Users\OPPILAS\Documents\Arduino\lampotilamittaus.txt', encoding="utf-8") as csv_file: | |||
csv_read=csv.reader(csv_file, delimiter=" ", quoting=csv.QUOTE_NONNUMERIC) | |||
for row in csv_read: | |||
results.append(row) | |||
#print( row ) | |||
#choose the data: | |||
col = 2 | |||
data = [] | |||
for row in results: | |||
#print( row[col] ) | |||
data.append( 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> | |||
=== Method 1 with more columns to be printed === | |||
The colums before adding one more T and the time: | |||
* 0, 8: Temperatures | |||
* 16, 18, 20: Accelerometer in xyz directions | |||
Change | |||
<syntaxhighlight lang="python"> | |||
#choose the data: | |||
cols = [16, 18, 20] | |||
data = [] | |||
for row in results: | |||
r = [] | |||
for c in cols: | |||
r.append( row[c] ) | |||
print( r ) | |||
data.append( r ) | |||
</syntaxhighlight> | |||
== References == |
Latest revision as of 19:35, 23 August 2023
Introduction
Many different methods exists. Here, we describe some. This use [[1]] data file. Download that and test the codes.
Theory of plotting log data
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
Does 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))
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()
Real time plotting
How to use Python to plot data acquired from the usb (COM) port. Will plot it real time, and save the data to be analyzed later.
Method 1 on Windows
import csv
import matplotlib.pyplot as plt
#Create the array to store the datA
results = []
with open(r'C:\Users\OPPILAS\Documents\Arduino\lampotilamittaus.txt', encoding="utf-8") as csv_file:
csv_read=csv.reader(csv_file, delimiter=" ", quoting=csv.QUOTE_NONNUMERIC)
for row in csv_read:
results.append(row)
#print( row )
#choose the data:
col = 2
data = []
for row in results:
#print( row[col] )
data.append( 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 1 with more columns to be printed
The colums before adding one more T and the time:
- 0, 8: Temperatures
- 16, 18, 20: Accelerometer in xyz directions
Change
#choose the data:
cols = [16, 18, 20]
data = []
for row in results:
r = []
for c in cols:
r.append( row[c] )
print( r )
data.append( r )