見出し画像

【やさしいpython講座】pythonを始めるならここから!

pythonの入門動画です!
自分のことを理系っぽいと思う方におススメです.

今回は後輩教育用のための動画です.
学部生向けに衛星に関わるコンテンツを入れつつ,pythonを始めてもらおうというコンセプトです.


動画データ

以下データです.



ソースコード

以下を上記のsensordata.csvと同じディレクトリにおいてください

import pandas as pd
import matplotlib.pyplot as plt
import scipy
import numpy as np

# -------------------------------------------------------------------------------
# pandas part
# 簡単にcsvを読み込んでみよう(特徴量も)
# -------------------------------------------------------------------------------

# pandas ver.確認
print(pd.__version__)
df = pd.read_csv("sensordata.csv")
# print(df.describe())

# -------------------------------------------------------------------------------
# matplotlib part
# 簡単に図示をしてみよう
# -------------------------------------------------------------------------------

# グラフの設定を行う
fig = plt.figure(figsize=(16,9))
ax_accx = fig.add_subplot(231,title=df.columns[1],xlabel = "time[sec]",ylabel=df.columns[1]+" [m/s^2]")
ax_accy = fig.add_subplot(232,title=df.columns[2],xlabel = "time[sec]",ylabel=df.columns[2]+" [m/s^2]")
ax_accz = fig.add_subplot(233,title=df.columns[3],xlabel = "time[sec]",ylabel=df.columns[3]+" [m/s^2]")
ax_gyrox = fig.add_subplot(234,title=df.columns[4],xlabel = "time[sec]",ylabel=df.columns[4]+" [rad/s]")
ax_gyroy = fig.add_subplot(235,title=df.columns[5],xlabel = "time[sec]",ylabel=df.columns[5]+" [rad/s]")
ax_gyroz = fig.add_subplot(236,title=df.columns[6],xlabel = "time[sec]",ylabel=df.columns[6]+" [rad/s]")

ax_accx.grid()
ax_accy.grid()
ax_accz.grid()
ax_gyrox.grid()
ax_gyroy.grid()
ax_gyroz.grid()

fig.savefig("sample.png")

# sampling timeを相対時刻に直す
passedTime = df["SamplingTime"] - df["SamplingTime"][0]
# print(passedTime)

# plot
ax_accx.plot(passedTime,df[" AccelerationX"])
ax_accy.plot(passedTime,df[" AccelerationY"])
ax_accz.plot(passedTime,df[" AccelerationZ"])
ax_gyrox.plot(passedTime,df[" GyroX"])
ax_gyroy.plot(passedTime,df[" GyroY"])
ax_gyroz.plot(passedTime,df[" GyroZ"])
fig.savefig("result_measure.png")

plt.clf()
plt.close()

# -------------------------------------------------------------------------------
# scipy part
# 数値解析を簡単に行おう
# -------------------------------------------------------------------------------

# 数値計算 scipy
gyrox_integral_simpson = scipy.integrate.simps(df[" GyroX"], passedTime)
gyroy_integral_simpson = scipy.integrate.simps(df[" GyroY"], passedTime)
gyroz_integral_simpson = scipy.integrate.simps(df[" GyroZ"], passedTime)
print("x軸周りの回転角 [rad] : "+str(gyrox_integral_simpson))
print("y軸周りの回転角 [rad] : "+str(gyroy_integral_simpson))
print("z軸周りの回転角 [rad] : "+str(gyroz_integral_simpson))

# -------------------------------------------------------------------------------
# numpy part
# 簡単にフーリエ変換してみよう
# -------------------------------------------------------------------------------

# フーリエ変換
N = len(df[" AccelerationX"][100:200]) # [100:200]
T = 0.1
freq = np.fft.fftfreq(N,T)
x_fft = np.fft.fft(df[" AccelerationX"][100:200])
y_fft = np.fft.fft(df[" AccelerationY"][100:200])
z_fft = np.fft.fft(df[" AccelerationZ"][100:200])

fig = plt.figure(figsize=(16,4))
ax_accx = fig.add_subplot(131,title=df.columns[1],xlabel = "freq [Hz]",ylabel=df.columns[1]+"")
ax_accy = fig.add_subplot(132,title=df.columns[2],xlabel = "freq [Hz]",ylabel=df.columns[2]+"")
ax_accz = fig.add_subplot(133,title=df.columns[3],xlabel = "freq [Hz]",ylabel=df.columns[3]+"")

ax_accx.set_xlim(0,5)
ax_accy.set_xlim(0,5)
ax_accz.set_xlim(0,5)
ax_accx.set_ylim(0,100)
ax_accy.set_ylim(0,100)
ax_accz.set_ylim(0,100)


ax_accx.plot(freq,np.abs(x_fft))
ax_accy.plot(freq,np.abs(y_fft))
ax_accz.plot(freq,np.abs(z_fft))

fig.savefig("result_fft.png")

plt.clf()
plt.close()

参考

#python のインストール

#VSCodeのインストール

#動画資料に関する説明

iOS、sensorlogger




人工衛星を大学で開発しています! いずれは宇宙や衛星,ロケットを題材とした高校生や一般向けの教材を作りたいと考えています!! もしよろしければ応援していただければと思います! 教材をkindleで売る費用や活動費(インタビュなど)で使わせていただきます.