顯示並標註圖表
如同你先前所看到的,若 pandas DataFrame 的索引是日期,pandas 會自動把 x 軸格式化為容易閱讀的方式。此外,.plot() 方法也能讓你指定多種參數,來客製化時間序列圖(例如線條顏色、線條寬度與圖形大小)。
你可能注意到 ax = df.plot(...) 這種寫法,並想知道 ax 物件的用途。這是因為 plot 函式會回傳一個 matplotlib 的 AxesSubplot 物件,慣例上會把這個回傳物件指定給名為 ax 的變數。這麼做也能讓你在圖上加入更多設定與標註,例如座標軸標籤。
本練習屬於課程
使用 Python 視覺化時間序列資料
練習說明
顯示 discoveries DataFrame 的折線圖。
- 將線條顏色指定為
'blue'。 - 將線條寬度設為 2。
- 圖表尺寸設為長 8、寬 3。
- 將
fontsize指定為 6。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Plot a line chart of the discoveries DataFrame using the specified arguments
ax = ____.____(____='blue', ____=(8, ____), ____=2, fontsize=____)
# Specify the title in your plot
ax.set_title('Number of great inventions and scientific discoveries from 1860 to 1959', fontsize=8)
# Show plot
plt.show()