「Raspberry Pi 3とBME280とPython3」の記事で得たデータををグラフにしたときのメモ。
環境
・Python ver.3.4.2
・Matplotlib ver.1.4.2-3.1
・Pandas ver.0.14.1-2
・Raspberry Pi 3 Model B+
・BME280(温湿度・気圧センサモジュール)
作成したグラフ
準備
sudo aptitude install python3-matplotlib sudo aptitude install python3-pandas
Matplotlib
Matplotlib — Visualization with Python
Pandas
ある期間のデータを抽出したい場合もあるので使う。
「import csv」でも同様のことはできる。
http://pandas.pydata.org/
おまじない
SSHなどでリモートログインした状態で、グラフを作成する場合に記述する。
https://matplotlib.org/faq/howto_faq.html
import matplotlib
matplotlib.use("Agg")
こ2行の記述がないと、以下のエラーが出る。
_tkinter.TclError: no display name and no $DISPLAY environment variable
CSV形式のデータ(抜粋)
日時、温度、湿度、気圧の順。
2017/01/28 08:23:01,23.844584695110484,1002.4343530595305,26.626886284585492 2017/01/28 08:24:01,23.868550154150057,1002.4123945898161,26.729813797110555 2017/01/28 08:25:01,24.12365656590555,1002.4313210969965,26.55321532964762 2017/01/28 08:26:02,24.190192476724064,1002.3767219026329,25.98150288542956 2017/01/28 08:27:01,24.122395222113482,1002.363130095847,26.908592495075034
ソース
# -*- coding: utf-8 -*-#
# SSHなどのリモートログイン用
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import matplotlib.pyplot as gridspec
import pandas as pd
datafile = 'data-bme280.csv'
# 「date」をDataTimeIndex型にする。
#df = pd.read_csv(datafile, header=None, names=['date', 'temperature', 'pressure', 'humidity'], index_col='date', parse_dates='date')
# 「date」を文字列型にする。
df = pd.read_csv(datafile, header=None, names=['date', 'temperature', 'pressure', 'humidity'], index_col='date')
#df.index = pd.to_datetime(df.index)
#df.index = df.index.format()
# 指定した日付のデータを抽出する
#print(df['2017/04/26'])
#pdlist = pd.date_range('2017-01-01', '2017-02-01')
#pdlist = pd.date_range('2017-01-01 10:00', periods=20, freq='H')
#pdlist = pd.date_range('2017-01-01 10:00', '2017-02-01 10:00', freq='W')
#pdlist = pd.date_range('2017-01-01 10:00', '2017-02-01 10:00', freq='3H')
#pdlist = pd.date_range('2017-01-01 10:00', '2017-12-31 10:00', freq='BM')
#pdlist = pd.date_range('2017-04-26 19:00', '2017-04-27 11:00')
#print(df.dtypes)
#print(df.index)
#print(df)
fig = plt.figure( figsize = (10, 8) )
fig.autofmt_xdate()
# matplotlib の余白情報を変更
# max:1
# 「plt.tight_layout」で代用。
#plt.subplots_adjust(wspace=1, hspace=0.5)
#plt.subplots_adjust( hspace=0.3 )
# 3行3列
gs = gridspec.GridSpec(3, 20)
#num_ax = 3
#fig, ax = plt.subplots( num_ax )
#fig.subplots_adjust(hspace=0.75)
ax0 = plt.subplot( gs[0,:] )
ax1 = plt.subplot( gs[1,:] )
ax2 = plt.subplot( gs[2,:] )
# 各グラフの描画
df['temperature'].plot(ax=ax0)
df['humidity'].plot(ax=ax1)
df['pressure'].plot(ax=ax2)
# 各グラフのタイトル
#ax0.set_title('Temperature')
#ax1.set_title('Humidity')
#ax2.set_title('Pressure')
# Y軸のラベル
#ax0.set_ylabel('Temperature', color='r')
ax0.set_ylabel('Temperature [℃]')
ax1.set_ylabel('Humidity [%]')
ax2.set_ylabel('Pressure [hPa]')
# X軸のラベルを削除
ax0.set_xlabel('')
ax1.set_xlabel('')
ax2.set_xlabel('')
# X軸の目盛り
# 最下段のみ表示
ax0.set_xticklabels('', rotation=45, fontsize="x-small")
ax1.set_xticklabels('', rotation=45, fontsize="x-small")
#ax2.set_xticklabels(df.index, rotation=45, fontsize="x-small")
# X軸のラベルを回転
for label in ax0.get_xmajorticklabels() + ax1.get_xmajorticklabels() + ax2.get_xmajorticklabels():
label.set_rotation( 50 )
label.set_horizontalalignment("right")
# subplotの余白を自動調整
plt.tight_layout()
#plt.show()
plt.savefig('raspi3-bme280.png')



Comments