matplotによってグラフ書き出しはできた。が、このグラフを毎回ディスプレイに表示なんてしないよな…画像ファイルにして日ごとに並べて比較していったほうがわかりやすくなる。となると、グラフを画像ファイルにして保存する必要がある。
ってことで、グラフを画像ファイルとして保存する。
#=============================== # DB内テーブル中身の確認(pandas) # matplotでグラフ書き出し # 複数グラフ作成 #=============================== import sqlite3 import pandas as pd import matplotlib.pyplot as plt #----------------------------- # 日付情報取得(ファイル名用) #----------------------------- import datetime td1 = datetime.date.today() td2 = td1.strftime('%Y%m%d') #----------------------------- # DB接続、カーソル起動 #----------------------------- dbname = 'TEST.db' connection = sqlite3.connect(dbname) cur = connection.cursor() #----------------------------- # データを2種類、DBから引き出す #----------------------------- # 0-24時までのデータ(data1) sql1 = 'SELECT temp, hour FROM WeatherTable LIMIT 24' c1 =cur.execute(sql1) result1 = c1.fetchall() #print(result1) # 次の日の0-24時までのデータ(data2) sql2 = 'SELECT temp, hour FROM WeatherTable LIMIT 24,24' c2 = cur.execute(sql2) result2 = c2.fetchall() #print(result2) #----------------------------- # 0-24時までのデータ(data3) sql3 = 'SELECT humid, hour FROM WeatherTable LIMIT 24' c3 =cur.execute(sql3) result3 = c3.fetchall() print(result3) # 次の日の0-24時までのデータ(data4) sql4 = 'SELECT humid, hour FROM WeatherTable LIMIT 24,24' c4 = cur.execute(sql4) result4 = c4.fetchall() print(result4) #----------------------------- # DataFrameにする #----------------------------- #データフレーム(表に書き出すイメージ) df1 = pd.DataFrame(result1,columns=["temp", "hour"]) df2 = pd.DataFrame(result2,columns=["temp", "hour"]) df3 = pd.DataFrame(result3,columns=["humid", "hour"]) df4 = pd.DataFrame(result4,columns=["humid", "hour"]) #----------------------------- # グラフの準備(2つのグラフ) #----------------------------- # グラフのX軸、Y軸の指定 x1 = df1.hour y1 = df1.temp x2 = df2.hour y2 = df2.temp x3 = df3.hour y3 = df3.humid x4 = df4.hour y4 = df4.humid # グラフ上限値を決めておく(5℃~25℃) tempMin = 5 tempMax = 25 humidMin = 0 humidMax = 100 # 表示エリアの設定 # fig内にchart1,chart2が横並びする形式にする # まずは、figのオブジェクト生成(名前もつける) fig = plt.figure(td2 + "_01") # (data1)---------------------------- # add_subplot(行、列、場所) chart1 = fig.add_subplot(1,2,1) # データプロット chart1.plot(x1, y1) # Y軸の最小、最大値を取得 #ymin, ymax = plt.ylim() plt.ylim(tempMin, tempMax) # グラフ内のラベル表記 chart1.set_title("Today") chart1.set_xlabel("Time") chart1.set_ylabel("Temperature(℃)") # (data2)---------------------------- # add_subplot(行、列、場所) chart2 = fig.add_subplot(1,2,2) # データプロット chart2.plot(x2, y2) # data1で取得したY軸情報をdata2に設定(同じにする) #plt.ylim(ymin, ymax) plt.ylim(tempMin, tempMax) # グラフ内のラベル表記 chart2.set_title("Tomorrow") chart2.set_xlabel("Time") chart2.set_ylabel("Temperature(℃)") # まずは、figのオブジェクト生成(名前もつける) fig2 = plt.figure(td2+"_02") # (data3)---------------------------- # add_subplot(行、列、場所) chart3 = fig2.add_subplot(1,2,1) # データプロット chart3.plot(x3, y3) # data1で取得したY軸情報をdata2に設定(同じにする) plt.ylim(humidMin, humidMax) # グラフ内のラベル表記 chart3.set_title("Today") chart3.set_xlabel("Time") chart3.set_ylabel("Humidity(%)") # (data4)---------------------------- # add_subplot(行、列、場所) chart4 = fig2.add_subplot(1,2,2) # データプロット chart4.plot(x4, y4) # data1で取得したY軸情報をdata2に設定(同じにする) plt.ylim(humidMin, humidMax) # グラフ内のラベル表記 chart4.set_title("Tomorrow") chart4.set_xlabel("Time") chart4.set_ylabel("Humidity(%)") #----------------------------- # グラフの設定 #----------------------------- fig.tight_layout() fig2.tight_layout() #----------------------------- # グラフの表示 #----------------------------- #plt.show() #----------------------------- # グラフの保存 #----------------------------- fig.savefig(td2 + "_fig_temp.jpg") fig2.savefig(td2 + "_fig_humid.jpg") """ #================== # ディスプレイにデータ表示 #================== # ディスプレイ表示省略しない pd.set_option("display.max_colwidth", None) pd.set_option("display.max_rows", None) # ディスプレイに表示 print(df1) print(df2) print(df3) print(df4) """ #================== #DB終了処理 #================== cur.close() connection.close() print('--------------------') print('Pandas and Matplot; end of line')
前回とほとんど同じで、ファイル保存のための命令にfig.savefig(td2 + "_fig_temp.jpg")が入っただけね。ファイル名は日付情報を取得してその文字列を引用している。ま、よくやる方法ね。あ、ファイルが上書きできてしまうなぁ…これではグラフのファイルを貯めることができないなぁ。。。後で考えよう。
それにしても…何も考えずにつけ足していくとスクリプトが縦長になって読みづらい・・・まぁ動作検証が目的だから仕方ないところ。
◆今回作成したグラフ(見た目はとくに変化ないけど)
今日と明日の気温はほとんど変わらず。明日の夕方以降はそれほど気温が下がらないみたい。
明日の湿度は50%以上なので程よい湿気という感じ。洗い物のし過ぎで指の皮がボロボロになるってのもないね。冬の乾燥する時期って人差し指とかボロボロになって剥けると痛いんだよなぁ…ユースキンを手放せなかったけど、これからの季節はもう要らなくなるかな?
あれ?明日の夜から湿度が高くなってるって、また雨降るのかぁ?