開始使用免費開始

為時間序列圖加上註解

在圖上加上註解,可以把圖中的重點資訊凸顯出來。以氣候變遷資料集為例,我們可能想標出相對溫度首次超過 1 攝氏度的日期。

為此,我們會使用 Axes 物件的 annotate 方法。在這個練習中,名為 climate_changeDataFrame 已經載入記憶體。請使用 Axes 方法,只以日期為自變數繪製相對溫度欄位,並對資料加上註解。

本練習屬於課程

Matplotlib 資料視覺化入門

檢視課程

練習說明

  • 使用 ax.plot 方法,將 DataFrame 的索引與 relative_temp 欄位繪製成圖。
  • 使用 annotate 方法,在位置 (pd.Timestamp('2015-10-06'), 1) 加上文字 '>1 degree'

動手互動練習

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

fig, ax = plt.subplots()

# Plot the relative temperature data
____

# Annotate the date at which temperatures exceeded 1 degree
ax.____(____, ____)

plt.show()
編輯並執行程式碼