📜  Seaborn 简介 – Python

📅  最后修改于: 2022-05-13 01:54:57.803000             🧑  作者: Mango

Seaborn 简介 – Python

Seaborn 是一个惊人的Python统计图形绘图可视化库。它提供了漂亮的默认样式和调色板,使统计图更具吸引力。它建立在 matplotlib 库之上,并且与 pandas 的数据结构紧密集成。
Seaborn 旨在使可视化成为探索和理解数据的核心部分。它提供了面向数据集的 API,以便我们可以在相同变量的不同视觉表示之间切换,以便更好地理解数据集。

Seaborn中不同类别的情节

绘图基本上用于可视化变量之间的关系。这些变量可以是完全数字的,也可以是像组、类或部门这样的类别。 Seaborn 将地块分为以下几类——

  • 关系图:该图用于理解两个变量之间的关系。
  • 分类图:该图处理分类变量以及如何可视化它们。
  • 分布图:该图用于检查单变量和双变量分布
  • 回归图: seaborn 中的回归图主要用于添加视觉指南,有助于在探索性数据分析期间强调数据集中的模式。
  • 矩阵图:矩阵是散点图的数组。
  • 多图网格:一种有用的方法是在数据集的不同子集上绘制同一图的多个实例

安装

对于Python环境:

pip install seaborn

对于 conda 环境:

conda install seaborn

依赖项

  • Python 3.6+
  • numpy (>= 1.13.3)
  • scipy (>= 1.0.1)
  • 熊猫 (>= 0.22.0)
  • matplotlib (>= 2.1.2)
  • 统计模型 (>= 0.8.0)

使用 seaborn 的一些基本绘图

分布图: Seaborn 分布图用于绘制直方图,还有一些其他变体,如 kdeplot 和 rugplot。

Python3
# Importing libraries
import numpy as np
import seaborn as sns
  
  
# Selecting style as white,
# dark, whitegrid, darkgrid 
# or ticks
sns.set(style="white")
  
# Generate a random univariate 
# dataset
rs = np.random.RandomState(10)
d = rs.normal(size=100)
  
# Plot a simple histogram and kde 
# with binsize determined automatically
sns.distplot(d, kde=True, color="m")


Python3
import seaborn as sns
  
  
sns.set(style="dark")
fmri = sns.load_dataset("fmri")
  
# Plot the responses for different\
# events and regions
sns.lineplot(x="timepoint",
             y="signal",
             hue="region",
             style="event",
             data=fmri)


Python3
import seaborn as sns
  
sns.set(style="ticks")
  
# Loading the dataset
df = sns.load_dataset("anscombe")
  
# Show the results of a linear regression
sns.lmplot(x="x", y="y", data=df)


输出:

线图:线图是 seaborn 库中最基本的图之一。该图主要用于以某种时间序列的形式,即以连续的方式将数据可视化。

Python3

import seaborn as sns
  
  
sns.set(style="dark")
fmri = sns.load_dataset("fmri")
  
# Plot the responses for different\
# events and regions
sns.lineplot(x="timepoint",
             y="signal",
             hue="region",
             style="event",
             data=fmri)

输出 :

Lmplot : lmplot 是另一个最基本的情节。它显示了代表线性回归模型的线以及 2D 空间上的数据点,x 和 y 可以分别设置为水平和垂直标签。

Python3

import seaborn as sns
  
sns.set(style="ticks")
  
# Loading the dataset
df = sns.load_dataset("anscombe")
  
# Show the results of a linear regression
sns.lmplot(x="x", y="y", data=df)

输出 :