📈
Python3 Pandas + Matplotlibを使用して、東京都の過去50年間の8月平均気温と移動平均を描画する
初めに
Python3でのデータ分析に少し興味を持ったので、データ分析ライブラリのPandasと、グラフ描画ライブラリのMatplotlibを使用し、2024年から過去50年間の東京都の8月の平均気温とその移動平均を算出してグラフに描画した方法を記載します。
作成したコードはJupyter Notebook上で動作し、グラフを描画しています。
なお、開発環境の構築方法などについては本記事では割愛します。
環境・使用ライブラリ
コード
import pandas as pd
import matplotlib.pyplot as plt
# 1975年から2024年までの東京都の8月の平均気温のデータを定義
df = pd.DataFrame([
{"年月": "1975年8月", "平均気温": 27.3},
{"年月": "1976年8月", "平均気温": 25.1},
{"年月": "1977年8月", "平均気温": 25.0},
{"年月": "1978年8月", "平均気温": 28.9},
{"年月": "1979年8月", "平均気温": 27.4},
{"年月": "1980年8月", "平均気温": 23.4},
{"年月": "1981年8月", "平均気温": 26.2},
{"年月": "1982年8月", "平均気温": 27.1},
{"年月": "1983年8月", "平均気温": 27.5},
{"年月": "1984年8月", "平均気温": 28.6},
{"年月": "1985年8月", "平均気温": 27.9},
{"年月": "1986年8月", "平均気温": 26.8},
{"年月": "1987年8月", "平均気温": 27.3},
{"年月": "1988年8月", "平均気温": 27.0},
{"年月": "1989年8月", "平均気温": 27.1},
{"年月": "1990年8月", "平均気温": 28.6},
{"年月": "1991年8月", "平均気温": 25.5},
{"年月": "1992年8月", "平均気温": 27.0},
{"年月": "1993年8月", "平均気温": 24.8},
{"年月": "1994年8月", "平均気温": 28.9},
{"年月": "1995年8月", "平均気温": 29.4},
{"年月": "1996年8月", "平均気温": 26.0},
{"年月": "1997年8月", "平均気温": 27.0},
{"年月": "1998年8月", "平均気温": 27.2},
{"年月": "1999年8月", "平均気温": 28.5},
{"年月": "2000年8月", "平均気温": 28.3},
{"年月": "2001年8月", "平均気温": 26.4},
{"年月": "2002年8月", "平均気温": 28.0},
{"年月": "2003年8月", "平均気温": 26.0},
{"年月": "2004年8月", "平均気温": 27.2},
{"年月": "2005年8月", "平均気温": 28.1},
{"年月": "2006年8月", "平均気温": 27.5},
{"年月": "2007年8月", "平均気温": 29.0},
{"年月": "2008年8月", "平均気温": 26.8},
{"年月": "2009年8月", "平均気温": 26.6},
{"年月": "2010年8月", "平均気温": 29.6},
{"年月": "2011年8月", "平均気温": 27.5},
{"年月": "2012年8月", "平均気温": 29.1},
{"年月": "2013年8月", "平均気温": 29.2},
{"年月": "2014年8月", "平均気温": 27.7},
{"年月": "2015年8月", "平均気温": 26.7},
{"年月": "2016年8月", "平均気温": 27.1},
{"年月": "2017年8月", "平均気温": 26.4},
{"年月": "2018年8月", "平均気温": 28.1},
{"年月": "2019年8月", "平均気温": 28.4},
{"年月": "2020年8月", "平均気温": 29.1},
{"年月": "2021年8月", "平均気温": 27.4},
{"年月": "2022年8月", "平均気温": 27.5},
{"年月": "2023年8月", "平均気温": 29.2},
{"年月": "2024年8月", "平均気温": 29.0}
])
df.set_index('年月', inplace=True)
# 移動平均を計算
df['5年移動平均'] = df['平均気温'].rolling(window=5).mean()
df['10年移動平均'] = df['平均気温'].rolling(window=10).mean()
df['20年移動平均'] = df['平均気温'].rolling(window=20).mean()
# 描画領域
plt.figure(figsize=(18, 9))
# 8月平均気温描画
plt.plot(df.index, df['平均気温'], label='8月平均気温', color='black', alpha=0.5, marker='.', markersize=10)
# 5,10,20移動平均線描画
plt.plot(df.index, df['5年移動平均'], label='5年移動平均', color='blue', marker='.', markersize=10)
plt.plot(df.index, df['10年移動平均'], label='10年移動平均', color='yellow', marker='.', markersize=10)
plt.plot(df.index, df['20年移動平均'], label='20年移動平均', color='red', marker='.', markersize=10)
# グラフレイアウト
plt.title('1975年〜2024年の東京都の8月平均気温と移動平均')
plt.xlabel('年月')
plt.ylabel('8月平均気温')
plt.legend(loc='upper left')
plt.gcf().autofmt_xdate()
plt.grid(True)
plt.tight_layout()
plt.show()
実行結果
Jupyter Notebook上で上記のコードを実行して描画されたグラフです。
ポイント
データ定義
pandas.DataFrame()
でグラフ描画に用いるデータの定義をしています。
# 1975年から2024年までの東京都の8月の平均気温のデータを定義
df = pd.DataFrame([
{"年月": "1975年8月", "平均気温": 27.3},
{"年月": "1976年8月", "平均気温": 25.1}# ...以下省略
])
移動平均の計算
rolling(window={計算する期間})
で期間を指定して、mean()
でその期間の平均を算出しています。
# 移動平均を計算
df['5年移動平均'] = df['平均気温'].rolling(window=5).mean()
df['10年移動平均'] = df['平均気温'].rolling(window=10).mean()
df['20年移動平均'] = df['平均気温'].rolling(window=20).mean()
8月平均気温と5,10,20年移動平均の描画
matplotlib.pyplot.plot()
を使用して、8月平均気温は黒色、5年移動平均は青色、10年移動平均は黄色、20年移動平均は赤色でそれぞれ折れ線グラフで描画しています。
# 8月平均気温描画
plt.plot(df.index, df['平均気温'], label='8月平均気温', color='black', alpha=0.5, marker='.', markersize=10)
# 5,10,20移動平均線描画
plt.plot(df.index, df['5年移動平均'], label='5年移動平均', color='blue', marker='.', markersize=10)
plt.plot(df.index, df['10年移動平均'], label='10年移動平均', color='yellow', marker='.', markersize=10)
plt.plot(df.index, df['20年移動平均'], label='20年移動平均', color='red', marker='.', markersize=10)
最後に
以上となります。
Pandas、Matplotlib、Jupyter Notebookも全て初めて使用したので躓いたところもありましたが、無事にグラフを描画できました。
単純なデータの折れ線グラフ表示とその移動平均線を描画しただけなので、すごく基本的な処理だとは思います。
別の機会に他の分析した結果を描画してみようと思います。
分析に使用した東京都の平均気温のデータ
出典:気象庁ホームページ (https://www.data.jma.go.jp/risk/obsdl/index.php)
Discussion