使用 seaborn 製作依類別的摘要統計圖

在 Python 中匯入與管理財務資料

Stefan Jansen

Instructor

使用 seaborn 的類別型圖表

  • 類別變數與數值變數的專用繪圖方式
  • 視覺化每個類別的摘要統計估計值
  • 了解類別如何影響數值變數
  • 以分佈特性指標進行比較
  • 範例:各產業或 IPO 年的平均市值與離散程度
在 Python 中匯入與管理財務資料

基礎:countplot

sns.countplot(x='Sector', data=nasdaq)
plt.xticks(rotation=45)

計數圖 countplot

在 Python 中匯入與管理財務資料

countplot,排序後

sector_size = nasdaq.groupby('Sector').size()
order = sector_size.sort_values(ascending=False)
order.head()
Sector
Health Care          645
Finance              627
Technology           433
...
order = order.index.tolist()
['Health Care', 'Finance', ..., 'Energy', 'Transportation']
在 Python 中匯入與管理財務資料

countplot,排序後

sns.countplot(x='Sector', data=nasdaq, order=order)
plt.xticks(rotation=45)
plt.title('# Observations per Sector')

排序後的計數圖

在 Python 中匯入與管理財務資料

countplot,多重類別

recent_ipos = nasdaq[nasdaq['IPO Year'] > 2014]
recent_ipos['IPO Year'] = recent_ipos['IPO Year'].astype(int)
sns.countplot(x='Sector', hue='IPO Year', data=recent_ipos)

多類別計數圖

在 Python 中匯入與管理財務資料

用 PointPlot 比較統計

nasdaq['IPO'] = nasdaq['IPO Year'].apply(lambda x: 'After 2000' if x > 2000 else 'Before 2000')
sns.pointplot(x='Sector', y='market_cap_m', hue='IPO', data=nasdaq)
plt.xticks(rotation=45); plt.title('Mean Market Cap')

PointPlot 比較統計

在 Python 中匯入與管理財務資料

一起來練習吧!

在 Python 中匯入與管理財務資料

Preparing Video For Download...