始める無料で始める

plt.subplots で小さな複数図を作成する

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

データは seattle_weatheraustin_weather という DataFrame として用意されています。それぞれに "MONTH" 列、平均降水量を表す "MLY-PRCP-NORMAL" 列、平均気温を表す "MLY-TAVG-NORMAL" 列があります。この演習では、各都市の月別平均降水量と平均気温を、それぞれ別のサブプロットに描画します。

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

Matplotlib によるデータ可視化入門

コースを見る

演習の手順

  • 2行2列の Figure とサブプロットの配列を作成します。
  • 左上の Axes(インデックス 0, 0)を指定し、シアトルの降水量を描画します。
  • 右上(インデックス 0, 1)には、シアトルの気温を描画します。
  • 左下(1, 0)と右下(1, 1)には、それぞれオースティンの降水量と気温を描画します。

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

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

# 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()
コードを編集して実行