開始使用免費開始

使用 plt.subplots 建立 small multiples

Small multiples 用來將多個資料集並排繪製。在 Matplotlib 中,可以使用 plt.subplots() 建立 small multiples。第一個引數是要產生之 Axes 物件陣列的列數,第二個引數是欄數。在這個練習中,你會利用 Austin 與 Seattle 的資料,練習建立並填入一個子圖陣列。

資料已提供為 DataFrame:seattle_weatheraustin_weather。它們各自都有一個 "MONTH" 欄,以及 "MLY-PRCP-NORMAL"(平均降水量)與 "MLY-TAVG-NORMAL"(平均氣溫)欄位。在本練習中,你會在各自的子圖中,繪製每個城市的每月平均降水量與平均氣溫。

本練習屬於課程

Matplotlib 資料視覺化入門

檢視課程

練習說明

  • 建立一個 Figure 與 2 列 2 欄的子圖陣列。
  • 將左上角的 Axes 視為索引 0, 0,繪製 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()
編輯並執行程式碼