共用 y 軸的小 multiples
在建立 small multiples 時,通常會希望不同的圖使用相同的 y 軸刻度。你可以將 sharey 關鍵字設為 True 來達成。
在這個練習中,你會建立一個含有兩個 Axes 物件且共用 y 軸的 Figure。和之前一樣,資料已放在 seattle_weather 與 austin_weather 這兩個 DataFrame 中。
本練習屬於課程
Matplotlib 資料視覺化入門
練習說明
- 建立一個含有兩個 Axes 物件陣列且共用 y 軸範圍的 Figure。
- 在上方的 Axes 以實線藍色繪製西雅圖的
"MLY-PRCP-NORMAL"。 - 在上方的 Axes 以藍色虛線加入西雅圖的
"MLY-PRCP-25PCTL"與"MLY-PRCP-75PCTL"。 - 在下方的 Axes 以實線紅色繪製奧斯汀的
"MLY-PRCP-NORMAL",並以紅色虛線繪製"MLY-PRCP-25PCTL"與"MLY-PRCP-75PCTL"。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Create a figure and an array of axes: 2 rows, 1 column with shared y axis
fig, ax = plt.subplots(2, 1, sharey=True)
# Plot Seattle precipitation data in the top axes
____.plot(____, ____, color = ____)
____.plot(____, ____, color = ____, linestyle = ____)
____.plot(____, ____, color = ____, linestyle = ____)
# Plot Austin precipitation data in the bottom axes
____.plot(____, ____, color = ____)
____.plot(____, ____, color = ____, linestyle = ____)
____.plot(____, ____, color = ____, linestyle = ____)
plt.show()