開始使用免費開始

將資料加入 Axes 物件

將資料加入圖形要呼叫 Axes 物件的方法。在這個練習中,你會使用 plot 方法,將兩個美國城市的降雨量資料加入圖上:華盛頓州的西雅圖(Seattle, WA)與德州的奧斯汀(Austin, TX)。

這些資料已載入記憶體,分別存放在兩個 pandas DataFrame:seattle_weather 儲存西雅圖的天氣資訊,austin_weather 儲存奧斯汀的天氣資訊。每個 DataFrame 都有一個 "MONTH" 欄位,存放月份的三個字母縮寫;也各有一個名為 "MLY-PRCP-NORMAL" 的欄位,存放 10 年期間每個月份的平均降雨量。

在這個練習中,你將建立一個視覺化圖表,用來比較這兩個城市的降雨量。

本練習屬於課程

Matplotlib 資料視覺化入門

檢視課程

練習說明

  • 導入 matplotlib.pyplot 子模組並命名為 plt
  • 透過呼叫 plt.subplots 建立一個 Figure 與一個 Axes 物件。
  • 呼叫 Axes 的 plot 方法,加入來自 seattle_weather DataFrame 的資料。
  • 以相同方式加入 austin_weather DataFrame 的資料,並呼叫 plt.show 顯示結果。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Import the matplotlib.pyplot submodule and name it plt
____

# Create a Figure and an Axes with plt.subplots
fig, ax = ____

# Plot MLY-PRCP-NORMAL from seattle_weather against the MONTH
ax.____(seattle_weather["MONTH"], ____)

# Plot MLY-PRCP-NORMAL from austin_weather against MONTH
ax.____(____, ____)

# Call the show function
____
編輯並執行程式碼