在時間序列資料中註記重要事件
當你繪製 jobs DataFrame 中 Finance、Information、Manufacturing 和 Construction 的時間序列時,你會發現 2001 年與 2008 年的失業率明顯上升。一般來說,如果在圖中加入額外的註記,強調特定觀察或事件,時間序列圖會更具資訊性。這能讓你快速向讀者標示圖中的重點區段,並協助推測造成特定事件的可能原因。
回想一下,你已經將 jobs DataFrame 的 datestamp 欄位設為索引,因此現在可以直接用垂直線或水平線在圖上加上註記。
本練習屬於課程
使用 Python 視覺化時間序列資料
練習說明
_ 在單一圖上繪製 jobs 中的所有時間序列,並使用 Spectral 配色。
- 在日期
2001-07-01加上一條藍色的垂直線。 - 在日期
2008-09-01再加上一條藍色的垂直線。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Plot all time series in the jobs DataFrame
ax = ____(____, fontsize=6, linewidth=0.8)
# Set labels and legend
ax.set_xlabel('Date', fontsize=10)
ax.set_ylabel('Unemployment Rate', fontsize=10)
ax.set_title('Unemployment rate of U.S. workers by industry', fontsize=10)
ax.legend(loc='center left', bbox_to_anchor=(1.0, 0.5))
# Annotate your plots with vertical lines
____(____, color='blue', linestyle='--', linewidth=0.8)
____(____, color='blue', linestyle='--', linewidth=0.8)
# Show plot
plt.show()