使用 matplotlib 自定义

Seaborn 数据可视化进阶

Chris Moffitt

Instructor

Matplotlib Axes

  • 大多数自定义可通过 matplotlibAxes 对象完成
  • 可将 Axes 传入 seaborn 函数
fig, ax = plt.subplots()
sns.histplot(df['Tuition'], ax=ax)
ax.set(xlabel='Tuition 2013-14')

示例 Axes 图

Seaborn 数据可视化进阶

更多自定义

  • axes 对象支持多种常见自定义
fig, ax = plt.subplots()
sns.histplot(df['Tuition'], ax=ax)
ax.set(xlabel="Tuition 2013-14",
       ylabel="Distribution", xlim=(0, 50000), 
title="2013-14 Tuition and Fees Distribution")

更多 Axes 示例

Seaborn 数据可视化进阶

组合图表

  • 可以组合并配置多个图
fig, (ax0, ax1) = plt.subplots(nrows=1, ncols=2, 
                               sharey=True, figsize=(7,4))

sns.histplot(df['Tuition'], stat='density', ax=ax0)
sns.histplot(df.query('State == "MN"')['Tuition'], stat='density', ax=ax1)

ax1.set(xlabel='Tuition (MN)', xlim=(0, 70000))
ax1.axvline(x=20000, label='My Budget', linestyle='--')
ax1.legend()
Seaborn 数据可视化进阶

组合图表

并排图

Seaborn 数据可视化进阶

让我们来练习!

Seaborn 数据可视化进阶

Preparing Video For Download...