Seaborn 简介

Seaborn 数据可视化进阶

Chris Moffitt

Instructor

Python 可视化生态

  • Python 可视化生态复杂,容易让人不知所措

Python 可视化生态

Seaborn 数据可视化进阶

Matplotlib

  • matplotlib 为 Seaborn 提供底层构件
  • 也可单独用于绘图
import matplotlib.pyplot as plt
import pandas as pd

df = pd.read_csv("wines.csv")
fig, ax = plt.subplots() ax.hist(df['alcohol'])

Matplotlib 直方图示例

Seaborn 数据可视化进阶

Pandas

  • pandas 是数据分析的基础库
  • 也支持基础绘图
import pandas as pd

df = pd.read_csv("wines.csv") df['alcohol'].plot.hist()

Pandas 直方图示例

Seaborn 数据可视化进阶

Seaborn

  • Seaborn 支持复杂的数据可视化
  • 基于 matplotlib,并与 pandas DataFrame 最兼容
Seaborn 数据可视化进阶

Seaborn histplot

  • histplot 类似前面示例中的直方图
  • 默认生成直方图,也可绘制更复杂的图
import seaborn as sns

sns.histplot(df['alcohol'])

Seaborn histplot 示例

Seaborn 数据可视化进阶

Seaborn displot

  • displot 基于 histplot 等函数绘制分布图
  • 默认生成直方图,也可生成其他类型图
import seaborn as sns

sns.displot(df['alcohol'], kind='kde')

Seaborn displot 示例

Seaborn 数据可视化进阶

pandas 直方图 vs. Displot

  • Pandas 直方图
    df['alcohol'].plot.hist()
    
    Pandas 直方图示例
    • 观测频数为准
    • 无柱形边框
    • 组距较宽
    • 无 x 轴标签
  • Seaborn displot
    sns.displot(df['alcohol'])
    
    Seaborn displot 示例
    • 自动 x 轴标签
    • 柔和配色
    • 更简洁
Seaborn 数据可视化进阶

开始练习吧!

Seaborn 数据可视化进阶

Preparing Video For Download...