使用 countplot() 繪製所有交易所的 IPO 時間軸
若要快速視覺化資料集中各類別的觀測數量,通常會使用 seaborn 的 countplot() 函式:
seaborn.countplot(x=None, hue=None, data=None, ...)
參數 x 放的是 data 引數(也就是要繪製的 DataFrame)中變數的名稱。hue 則以顏色標示另一個類別變數。以上是此函式眾多參數中的三個可選參數;完整清單請見 seaborn 的文件。
現在用這個工具來比較三大交易所的 IPO 活動時間軸。pandas 作為 pd、matplotlib.pyplot 作為 plt、以及 seaborn 作為 sns 都已匯入,且含有參考欄位 'Exchange' 的 listings DataFrame 已在你的工作區中可用。
本練習屬於課程
在 Python 中匯入與管理財務資料
練習說明
- 將
listings篩選為只包含 2000 年之後的 IPO 年份。 - 將欄位
'IPO Year'的資料轉成整數。 - 繪製
listings的sns.countplot(),x變數使用'IPO Year',hue使用'Exchange'。 - 將
xticks()旋轉 45 度並顯示結果。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Select IPOs after 2000
listings = listings[____[____] > ____]
# Convert IPO Year to integer
listings['IPO Year'] = ____[____].____(____)
# Create a countplot
sns.countplot(x=____, hue=____, data=____)
# Rotate xticks and show result
plt.xticks(rotation=45)
# Show the plot
plt.show()