Seaborn学习笔记(二)

之前学习了解的都是Seaborn的数据绘制时候的美化功能,只是为了使得绘制出的图形更加好看,Seaborn同样提供了许多的用于展示数据相互关系的绘图函数。

Seaborn绘制数据集的分布

1. 绘制单变量分布
最简便快速查看seaborn中单变量分布的是distplot()方法。默认情况下,这将绘制数据的直方图并拟合核心密度估计值(KDE)。

x = np.random.normal(size=100)
sns.distplot(x);

绘制单变量分布图

上边是使用distplot()绘制了一个变量分布的总体密度估计图,我们也可以选择在直方图中显示出具体每个数据在直方图区间的位置

#还是使用随机生成的数据,但是设置rug为true
x = np.random.normal(size=100)
sns.distplot(x, kde=False, rug=True);

我们可以看到具体的数据的密度分布情况,而不是拟合的曲线
具体的密度分布

同样的,我们也可以只显示具体数据的密度分布和他的对应拟合曲线

x = np.random.normal(size=100)
sns.distplot(x, hist=False, rug=True);

密度分布及拟合曲线

若是我们只是需要绘制数据分布密度的概览,而不是需要绘制直方图,我们可以直接使用Seaborn中提供的kdeplot()函数,上边的distplot()也是通过调用它来显示密度分布拟合曲线的

#直接显示密度分布的拟合曲线,而不是绘制直方图
sns.kdeplot(x, shade=True);

概率密度拟合曲线

我们可以通过bw参数来控制拟合曲线的精细程度,就类似于绘制直方图时的bins参数一样

#使用默认的bw参数
sns.kdeplot(x)
#调整bw参数,使得显示出来的密度分布拟合曲线更加接近数据
sns.kdeplot(x, bw=.2, label="bw: 0.2")
sns.kdeplot(x, bw=2, label="bw: 2")
plt.legend();

调整拟合的精细程度

2. 绘制双变量分布
Seaborn提供了用于描述双变量分布情况的绘图函数,可以直观的查看双变量的分布情况。该函数创建一个多面板图形,显示两个变量之间的二元(或关节)关系以及每个单独轴上的单变量(或边际)分布。

sns.jointplot(x="x", y="y", data=df);

双变量的散点图

此外Seaborn还提供了直方图中的二元关系图,也常被称为“六边形图”,该图对于较大的数据集效果最好。

x, y = np.random.multivariate_normal(mean, cov, 1000).T
with sns.axes_style("white"):
    sns.jointplot(x=x, y=y, kind="hex", color="k");

六边形图

也可以使用核密度图来估计二元变量的关系,在Seaborn中会以轮廓图的形式显示出来

sns.jointplot(x="x", y="y", data=df, kind="kde");

核密度图表示二元关系

Seaborn也允许将这类型的图绘制到已经用matplotlib绘制出来的轴上

#先用matplotlib绘制出坐标轴
f, ax = plt.subplots(figsize=(6, 6))
#用Seaborn将需要展示的图绘制到matplotlib轴上
sns.kdeplot(df.x, df.y, ax=ax)
sns.rugplot(df.x, color="g", ax=ax)
sns.rugplot(df.y, vertical=True, ax=ax);

效果图展示

可以使用kdeplot()中的n_levels参数来控制轮廓的数量,使得图片显示的更加连续或者是稀疏

f, ax = plt.subplots(figsize=(6, 6))
#用n_levels参数控制显示的疏密程度
sns.kdeplot(df.x, df.y, ax=ax,n_levels=100)
sns.rugplot(df.x, color="g", ax=ax)
sns.rugplot(df.y, vertical=True, ax=ax);

效果图

3. 展示数据集中的成对关系
要在数据集中绘制多个成对的双变量分布,可以使用该pairplot()函数。这将创建一个轴矩阵并显示DataFrame中每对列的关系。默认情况下,它也绘制每个变量在对角轴上的单变量分布。

一下使用的是鸢尾花数据集来展示这一效果

iris = sns.load_dataset("iris")
sns.pairplot(iris);

可以看到起使用多个子图来展示两个特征的数据之间的相互关系,同时也在对角线上绘制了每个单变量的分布情况。
效果图

1. 目录 1. 目录 2 2. 绘图函数Plotting functions 4 2.1. 可视化的统计关系Visualizing statistical relationships 4 2.1.1. 用散点图联系变量Relating variables with scatter plots 4 2.1.2. 强调线条图的连续性Emphasizing continuity with line plots 10 2.1.3. 显示与切面的多个关系Showing multiple relationships with facets 21 2.2. 分类数据绘图Plotting with categorical data 24 2.2.1. 分类散点图Categorical scatterplots 26 2.2.2. 分类观测值分布Distributions of observations within categories 31 2.2.3. 分类统计估计Statistical estimation within categories 37 2.2.4. 对“wide-form”数据作图Plotting “wide-form” data 41 2.2.5. 显示与facet的多个关系Showing multiple relationships with facets 43 2.3. 可视化数据集的分布Visualizing the distribution of a dataset 44 2.3.1. 绘制单变量分布Plotting univariate distributions 45 2.3.2. 绘制元分布Plotting bivariate distributions 51 2.3.3. 在数据集中可视化成对关系Visualizing pairwise relationships in a dataset 55 2.4. 可视化线性关系Visualizing linear relationships 57 2.4.1. 函数绘制线性模型Functions to draw linear regression models 58 2.4.2. 拟合不同种类的模型Fitting different kinds of models 61 2.4.3. 在其他变量上的情况Conditioning on other variables 68 2.4.4. 控制图表的大小和形状Controlling the size and shape of the plot 71 2.4.5. 在其他上下文中绘制回归图Plotting a regression in other contexts 73 3. 多图网格Multi-plot grids 76 3.1. 构建结构化的多图网格Building structured multi-plot grids 76 3.2. 有条件的小倍数Conditional small multiples 77 3.3. 使用定制函数Using custom functions 86 3.4. 绘制成对的数据关系Plotting pairwise data relationships 90 4. 绘图美学Plot aesthetics 99 4.1. 控制图表美学Controlling figure aesthetics 99 4.1.1. Seaborn图表风格Seaborn figure styles 101 4.1.2. 删除轴上的小凸起Removing axes spines 104 4.1.3. 临时设置图表样式Temporarily setting figure style 105 4.1.4. 覆盖Seaborn样式的元素Overriding elements of the seaborn styles 106 4.1.5. 缩放图表元素Scaling plot elements 108 4.2. 选择调色板Choosing color palettes 111 4.2.1. 创建颜色调色板Building color palettes 111 4.2.2. 定性调色板Qualitative color palettes 112 4.2.3. 连续调色板Sequential color palettes 116 4.2.4. 不同颜色的调色板Diverging color palettes 122 4.2.5. 设置默认调色板Setting the default color palette 124 5. 教程中的数据集 125
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值