時系列データのプロット:すべてをまとめる
この演習では、スケールの異なる2つの時系列を同じ Axes 上にプロットし、そのうち一方のデータに注釈を付けます。
CO2濃度と気温のデータは、climate_change という DataFrame として用意されています。また、以前定義した plot_timeseries 関数も使用します。この関数は Axes オブジェクト(axes 引数)を受け取り、時系列(x と y 引数で指定)をプロットし、x軸とy軸のラベルを設定し、データの色とy軸目盛り・ラベルの色を設定します。
plot_timeseries(axes, x, y, color, xlabel, ylabel)
その後、データ内の重要な時点にテキストで注釈を付けます。それは2015年10月6日、気温が平均より1度以上上昇した最初の日です。
この演習はコースの一部です
Matplotlib によるデータ可視化入門
演習の手順
plot_timeseries関数を使ってCO2濃度を時間にプロットします。xlabelを"Time (years)"に、ylabelを"CO2 levels"に、colorを'blue'に設定します。- 最初のAxesの双子として
ax2を作成します。 ax2では、温度を時間にプロットし、ylabelを"Relative temp (Celsius)"に、colorを'red'に設定します。ax2.annotateメソッドを使ってデータに注釈を付けます。テキスト">1 degree"をx=pd.Timestamp('2008-10-06')、y=-0.2に置き、灰色の細い矢印でx=pd.Timestamp('2015-10-06')、y=1を指します。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
fig, ax = plt.subplots()
# Plot the CO2 levels time-series in blue
plot_timeseries(____, ____, ____, 'blue', ____, ____)
# Create an Axes object that shares the x-axis
ax2 = ____
# Plot the relative temperature data in red
plot_timeseries(____, ____, ____, 'red', ____, ____)
# Annotate point with relative temperature >1 degree
ax2.____(">1 degree", ____, ____, ____)
plt.show()