使用 Seaborn 的類別圖入門

在 Python 中處理類別資料

Kasey Jones

Research Data Scientist

第三個資料集

  • 名稱:拉斯維加斯 TripAdvisor 評論 - reviews
  • 列數:504
  • 欄數:20
在 Python 中處理類別資料

拉斯維加斯評論

reviews.info()
RangeIndex: 504 entries, 0 to 503
Data columns (total 20 columns):
 #   Column             Non-Null Count  Dtype 
     ------             --------------  ----- 
 0   User country       504 non-null    object
...
 6   Traveler type      504 non-null    object
 7   Pool               504 non-null    object
 8   Gym                504 non-null    object
 9   Tennis court       504 non-null    object
...
dtypes: int64(7), object(13)
memory usage: 78.9+ KB
1 https://www.kaggle.com/crawford/las-vegas-tripadvisor-reviews
在 Python 中處理類別資料

Seaborn

類別圖:

import seaborn as sns
import matplotlib.pyplot as plt

sns.catplot(...)
plt.show()
在 Python 中處理類別資料

catplot 函式

參數:

  • xdata 中的變數名稱
  • ydata 中的變數名稱
  • data:一個 DataFrame
  • kind:要建立的圖形類型,可為:"strip""swarm""box""violin""boxen""point""bar""count"
在 Python 中處理類別資料

盒狀圖(Box plot)

在 Python 中處理類別資料

評論分數

reviews["Score"].value_counts()
5    227
4    164
3     72
2     30
1     11
在 Python 中處理類別資料

盒狀圖範例

sns.catplot(
  x="Pool",
  y="Score",
  data=reviews,
  kind="box"
)
plt.show()

在 Python 中處理類別資料

兩個快速選項

# 設定字型大小與圖表背景
sns.set(font_scale=1.4)

sns.set_style("whitegrid")
sns.catplot(
  x="Pool",
  y="Score",
  data=reviews,
  kind="box"
)
plt.show()

在 Python 中處理類別資料

盒狀圖練習

在 Python 中處理類別資料

Preparing Video For Download...