始める無料で始める

plt.subplots でスモールマルチプルを作成する

スモールマルチプルは、複数のデータセットを横に並べて可視化する手法です。Matplotlib では、plt.subplots() 関数を使ってスモールマルチプルを作成できます。最初の引数は生成する Axes オブジェクト配列の行数、2 番目の引数は列数です。この演習では、Austin と Seattle のデータを使って、サブプロット配列の作成と描画を練習します。

データは DataFrame の seattle_weatheraustin_weather として与えられています。どちらにも "MONTH" 列、平均降水量を表す "MLY-PRCP-NORMAL"、および平均気温を表す "MLY-TAVG-NORMAL" の各列があります。この演習では、各都市の月別平均降水量と平均気温を、別々のサブプロットに描画します。

この演習はコースの一部です

Matplotlibで学ぶデータ可視化入門

コースを見る

演習の手順

  • 2 行 2 列の Figure とサブプロット配列を作成します。
  • 左上(インデックス 0, 0)の Axes に、Seattle の降水量をプロットします。
  • 右上(インデックス 0, 1)に、Seattle の気温をプロットします。
  • 左下(1, 0)と右下(1, 1)に、Austin の降水量と気温をそれぞれプロットします。

実践的なインタラクティブ演習

このサンプルコードを完成させて、この演習に挑戦してみましょう。

# Create a Figure and an array of subplots with 2 rows and 2 columns
fig, ax = plt.subplots(____, ____)

# Addressing the top left Axes as index 0, 0, plot month and Seattle precipitation
ax[0, 0].plot(____, ____)

# In the top right (index 0,1), plot month and Seattle temperatures
ax[0, 1].plot(____, ____)

# In the bottom left (1, 0) plot month and Austin precipitations
ax[____].plot(____, ____)

# In the bottom right (1, 1) plot month and Austin temperatures
ax[____].plot(____, ____)
plt.show()
コードを編集して実行