在圖表中加入陰影區域
在 Python 中繪製時間序列時,你也可以突顯圖中的整段區域。若要在 1936 年 1 月 1 日到 1950 年 1 月 1 日之間加入一個陰影區域,可以使用下列指令:
ax.axvspan('1936-01-01', '1950-01-01', color='red' , alpha=0.5)
這裡我們用 alpha 參數來設定該區域的整體透明度(0 代表完全透明,1 代表不透明的實色)。
本練習屬於課程
使用 Python 視覺化時間序列資料
練習說明
- 使用
.axvspan()方法,在 1900 年 1 月 1 日到 1915 年 1 月 1 日之間加入紅色的垂直陰影區塊,透明度設為0.3。 - 使用
.axhspan()方法,在數值 6 到 8 之間加入綠色的水平陰影區塊,透明度設為0.3。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Plot your the discoveries time series
ax = discoveries.plot(color='blue', fontsize=6)
# Add a vertical red shaded region
ax.____('1900-01-01', ____, color=____, alpha=____)
# Add a horizontal green shaded region
ax.____(6, ____, color=____, alpha=____)
plt.show()