自訂座標軸標籤並加入標題
自訂座標軸標籤需要使用 Axes 物件的 set_xlabel 與 set_ylabel 方法。加入標題則使用 set_title 方法。
在這個練習中,你會自訂座標軸標籤的內容,並為圖表加入標題。
和先前一樣,資料已經以 pandas DataFrame 物件載入記憶體:seattle_weather 與 austin_weather。它們各自包含 "MONTH" 欄與 "MLY-PRCP-NORMAL" 欄。前兩行範例程式碼已經將這些資料彼此對照繪圖。
此外,名稱為 fig 的 Figure 物件與名稱為 ax 的 Axes 物件也已經為你建立好。
本練習屬於課程
Matplotlib 資料視覺化入門
練習說明
- 使用
set_xlabel方法新增標籤:"Time (months)"。 - 使用
set_ylabel方法新增標籤:"Precipitation (inches)"。 - 使用
set_title方法新增標題:"Weather patterns in Austin and Seattle"。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
ax.plot(seattle_weather["MONTH"], seattle_weather["MLY-PRCP-NORMAL"])
ax.plot(austin_weather["MONTH"], austin_weather["MLY-PRCP-NORMAL"])
# Customize the x-axis label
____
# Customize the y-axis label
____
# Add the title
____
# Display the figure
plt.show()