使用 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 中处理分类数据

箱线图

在 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 中处理分类数据

两个快速选项

# Setting font size and plot background
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...