• seaborn学习笔记(四):箱型图、小提琴图


     

    In [1]:
    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    import seaborn as sns
    
    In [3]:
    tips = sns.load_dataset("tips")
    
    In [4]:
    tips.head(2)
    
    Out[4]:
     
     total_billtipsexsmokerdaytimesize
    0 16.99 1.01 Female No Sun Dinner 2
    1 10.34 1.66 Male No Sun Dinner 3

    1 箱型图

    箱形图(Box-plot)又称为盒须图、盒式图或箱线图,是一种用作显示一组数据分散情况资料的统计图。在seaborn中,boxplot()方法用于绘制箱型图。boxplot()主要参数如下:

    • x,y:指定的x轴, y轴数据,可以是向量或者字符串,当是字符串时,一定是data中的一个key
    • hue:可以是向量(pandas中的一列,或者是list),也可以是字符串(data中的一个key),seaborn将根据这一列设置不同颜色
    • data:绘图数据,可以是pandas.DataFrame,numpy.ndarray或者字典等
    • order:包含所有分组属性的列表,用于指定条形顺序,注意如果某个分组不在order中,该分组将不会显示
    • hue_order:字符串组成的list,设置hue后设置各颜色顺序
    • orient:当x,y都是离散型或者数值型数据时,通过orient可设置图像方向
    • color:统一设置所有箱体的颜色
    • palette:颜色面板
    • saturation:颜色饱和度
    • width:设置箱体宽度
    • dodge:是否重叠绘制(设置hue之后生效)
    • fliersize:箱体上方离群点的大小
    • linewidth:箱体边框线的粗细
    • whis:确定离群值的上下界(IQR超过低和高四分位数的比例),此范围之外的点将被识别为异常值。IQR指的是上下四分位的差值。

    1.1 x, y:传递数据,控制图形方向

    x, y为绘图时指定的x轴和y轴数据,x、y如果有一个是离散型数据(或者说定性数据、分类数据),另一个是连续性数值数据,seaborn将会根据其中的离散型数据对另一个连续性数据进行绘制其在另一条数轴上的分布。

    In [5]:
    fig, ax =plt.subplots(1,2,constrained_layout=True, figsize=(8, 3))
    pic = sns.boxplot(x="day", y="total_bill", data=tips, ax=ax[0])
    pic.set_title('x="day", y="total_bill"')
    pic = sns.boxplot(x="total_bill", y="day", data=tips, ax=ax[1])
    pic.set_title('x="total_bill", y="day"')
    
    Out[5]:
    Text(0.5, 1.0, 'x="total_bill", y="day"')
     

    1.2 hue与dodge:根据指定指定绘制箱体颜色

    hue和dodge常常需要一起配合使用,hue只是根据指定字段绘制不同颜色的散点,进一步地,dodge为Flase可以将不同颜色的箱体重叠。

    In [18]:
    fig, ax =plt.subplots(1,2,constrained_layout=True, figsize=(8, 3))
    pic = sns.boxplot(x="day", y="total_bill", data=tips, ax=ax[0], hue="smoker")
    pic.set_title('hue="smoker"')
    pic = sns.boxplot(x="day", y="total_bill", data=tips, ax=ax[1], hue="smoker", dodge=False)
    pic.set_title('hue="smoker", dodge=False')
    
    Out[18]:
    Text(0.5, 1.0, 'hue="smoker", dodge=False')
     

    1.3 order:指定条形顺序

    In [8]:
    fig, ax =plt.subplots(1,2,constrained_layout=True, figsize=(8, 3))
    pic = sns.boxplot(x="day", y="total_bill", data=tips, ax=ax[0])
    pic.set_title('default order')
    pic = sns.boxplot(x="day", y="total_bill", data=tips, order=['Sun', 'Sat', 'Thur', 'Fri'], ax=ax[1])
    pic.set_title('"Sun", "Sat", "Thur", "Fri"')
    
    Out[8]:
    Text(0.5, 1.0, '"Sun", "Sat", "Thur", "Fri"')
     

    1.4 color,saturation:设置条形颜色和饱和度

    注意,color只能一次性设置所有条形统一的颜色,如果要为条形设置不同颜色,要通过palette参数:

    In [11]:
    ax = sns.boxplot(x="day", y="total_bill", data=tips, color="red")
    
     
    In [12]:
    ax = sns.boxplot(x="day", y="total_bill", data=tips, color="red",saturation=0.5 )
    
     

    1.5 width:设置箱体宽度

    In [15]:
    fig, ax =plt.subplots(1,2,constrained_layout=True, figsize=(8, 3))
    pic = sns.boxplot(x="day", y="total_bill", data=tips, ax=ax[0])
    pic.set_title('default width')
    pic = sns.boxplot(x="day", y="total_bill", data=tips, ax=ax[1], width=0.5)
    pic.set_title('width=0.5')
    
    Out[15]:
    Text(0.5, 1.0, 'width=0.5')
     

    1.6 fliersize:箱体上方离群点的大小

    In [23]:
    fig, ax =plt.subplots(1,2,constrained_layout=True, figsize=(8, 3))
    pic = sns.boxplot(x="day", y="total_bill", data=tips, ax=ax[0])
    pic.set_title('default fliersize: 5')
    pic = sns.boxplot(x="day", y="total_bill", data=tips, ax=ax[1], fliersize=1)
    pic.set_title('fliersize=1')
    
    Out[23]:
    Text(0.5, 1.0, 'fliersize=1')
     

    1.7 linewidth:箱体边框线的粗细

    In [28]:
    fig, ax =plt.subplots(1,3,constrained_layout=True, figsize=(8, 3))
    pic = sns.boxplot(x="day", y="total_bill", data=tips, ax=ax[0])
    pic.set_title('default linewidth')
    pic = sns.boxplot(x="day", y="total_bill", data=tips, ax=ax[1], linewidth=1)
    pic.set_title('linewidth=1')
    pic = sns.boxplot(x="day", y="total_bill", data=tips, ax=ax[2], linewidth=5)
    pic.set_title('linewidth=5')
    
    Out[28]:
    Text(0.5, 1.0, 'linewidth=5')
     

    1.8 whis:确定离群值的上下界

    In [30]:
    fig, ax =plt.subplots(1,3,constrained_layout=True, figsize=(8, 3))
    pic = sns.boxplot(x="day", y="total_bill", data=tips, ax=ax[0])
    pic.set_title('default whis: 1.5')
    pic = sns.boxplot(x="day", y="total_bill", data=tips, ax=ax[1], whis=2)
    pic.set_title('whis=2')
    pic = sns.boxplot(x="day", y="total_bill", data=tips, ax=ax[2], whis=3)
    pic.set_title('whis=3')
    
    Out[30]:
    Text(0.5, 1.0, 'whis=3')
     

    2 增强箱型图:boxenplot()

    boxenplot()是为更大的数据集绘制增强的箱型图。这种风格的绘图最初被命名为“信值图”,因为它显示了大量被定义为“置信区间”的分位数。它类似于绘制分布的非参数表示的箱形图,其中所有特征对应于实际观察的数值点。通过绘制更多分位数,它提供了有关分布形状的更多信息,特别是尾部数据的分布。

    作为增强版的boxplot(),boxenplot()大部分参数和boxplot()是相似的。现在就剩下不同的参数进行说明:

    • k_depth:“proportion” 或 “tukey” 或 “trustworthy”,也可以是标量整数,通过增大百分比的粒度控制绘制的盒形图数目。每个参数代表利用不同的统计特性对异常值的数量做出不同的假设。

    • scale:“linear” 或 “exponential” 或 “area”,用于控制增强箱型图宽度的方法。所有参数都会给显示效果造成影响。 “linear” 通过恒定的线性因子减小宽度,“exponential” 使用未覆盖的数据的比例调整宽度, “area” 与所覆盖的数据的百分比成比例。

    • outlier_prop:float,被认为是异常值的数据比例。与 k_depth 结合使用以确定要绘制的百分位数。默认值为 0.007 作为异常值的比例。该参数取值应在[0,1]范围内。

    boxenplot()用的不多,大部分参数与boxplot()一样,不再演示,增强箱型图外观如下图所示:

    In [53]:
    _ = sns.boxenplot(x="day", y="total_bill", data=tips)
    
     

    3 小提琴图:violinplot()

    violinplot()是用来绘制箱形图和核密度估计组合图,即小提琴图。小提琴形图的作用与箱形图类似,它显示了一个或多个分类变量的几个级别的定量数据的分布,我们可以通过观察来比较这些分布。与盒形图不同,因为盒形图的所有绘图组件都对应于实际数据点,小提琴形图具有底层分布的核密度估计。

    • x,y:指定的x轴, y轴数据,可以是向量或者字符串,当是字符串时,一定是data中的一个key
    • hue:可以是向量(pandas中的一列,或者是list),也可以是字符串(data中的一个key),seaborn将根据这一列设置不同颜色
    • data:绘图数据,可以是pandas.DataFrame,numpy.ndarray或者字典等
    • order:包含所有分组属性的列表,用于指定条形顺序,注意如果某个分组不在order中,该分组将不会显示
    • hue_order:字符串组成的list,设置hue后设置各颜色顺序
    • bw:'scott','silverman'或float数值,计算内核带宽时使用的引用规则的名称或比例因子。实际内核大小将通过将比例因子乘以每个bin中数据的标准差来确定。
    • cut:以带宽大小为单位的距离,用于将密度扩展到超过极端数据点。设置为0可将小提琴范围限制在观测数据范围内(即,与ggplot中的trim=true具有相同的效果)
    • scale:“area”, “count”, “width”,用于缩放每个小提琴宽度。如果是“area”,每个小提琴都会有相同的区域。如果“count”,小提琴的宽度将按照该箱中的观察次数进行缩放。如果“width”,每个小提琴将具有相同的宽度
    • gridsize:用于计算核密度估计的离散网格中的点数
    • width:设置箱体宽度
    • inner:'box','quartile','point','stick',None,表示小提琴内部的数据点。如果是框,画一个微型箱图。如果是四分位数,则绘制分布的四分位数。如果point或stick,则显示每个基础数据点。使用None将绘制未经修饰的小提琴
    • split:当使用带有两个级别的变量的色调嵌套(hue)时,将split设置为True将为每个级别绘制一半小提琴。这样可以更容易比较分布。
    • dodge:是否重叠绘制(设置hue之后生效)
    • orient:当x,y都是离散型或者数值型数据时,通过orient可设置图像方向
    • linewidth:箱体边框线的粗细
    • color:统一设置所有箱体的颜色
    • palette:颜色面板
    • saturation:颜色饱和度
    • ax:自定义坐标系

    3.1 x, y:传递数据,控制图形方向

    x, y为绘图时指定的x轴和y轴数据,x、y如果有一个是离散型数据(或者说定性数据、分类数据),另一个是连续性数值数据,seaborn将会根据其中的离散型数据对另一个连续性数据进行绘制其在另一条数轴上的分布。

    In [59]:
    fig, ax =plt.subplots(1,2,constrained_layout=True, figsize=(8, 3))
    pic = sns.violinplot(x="day", y="total_bill", data=tips, ax=ax[0])
    pic.set_title('x="day", y="total_bill"')
    pic = sns.violinplot(x="total_bill", y="day", data=tips, ax=ax[1])
    _ = pic.set_title('x="total_bill", y="day"')
    
     

    3.2 hue与dodge:根据指定指定绘制箱体颜色

    hue和dodge常常需要一起配合使用,hue只是根据指定字段绘制不同颜色的散点,进一步地,dodge为Flase可以将不同颜色的箱体重叠。

    In [60]:
    fig, ax =plt.subplots(1,2,constrained_layout=True, figsize=(8, 3))
    pic = sns.violinplot(x="day", y="total_bill", data=tips, ax=ax[0], hue="smoker")
    pic.set_title('hue="smoker"')
    pic = sns.violinplot(x="day", y="total_bill", data=tips, ax=ax[1], hue="smoker", dodge=False)
    _ = pic.set_title('hue="smoker", dodge=False')
    
     

    3.3 order:指定条形顺序

    In [61]:
    fig, ax =plt.subplots(1,2,constrained_layout=True, figsize=(8, 3))
    pic = sns.violinplot(x="day", y="total_bill", data=tips, ax=ax[0])
    pic.set_title('default order')
    pic = sns.violinplot(x="day", y="total_bill", data=tips, order=['Sun', 'Sat', 'Thur', 'Fri'], ax=ax[1])
    _ = pic.set_title('"Sun", "Sat", "Thur", "Fri"')
    
     

    3.4 color与saturation:设置条形颜色和饱和度

    注意,color只能一次性设置所有条形统一的颜色,如果要为条形设置不同颜色,要通过palette参数:

    In [63]:
    ax = sns.violinplot(x="day", y="total_bill", data=tips, color="red")
    
     
    In [64]:
    ax = sns.violinplot(x="day", y="total_bill", data=tips, color="red",saturation=0.5 )
    
     

    3.5 split:使用hue后是否单独绘图

    In [73]:
    fig, ax =plt.subplots(1,2,constrained_layout=True, figsize=(12, 3))
    pic = sns.violinplot(x="day", y="total_bill", data=tips, ax=ax[0], hue="smoker")
    pic.set_title('no split')
    pic = sns.violinplot(x="day", y="total_bill", data=tips, ax=ax[1], hue="smoker", split=True)
    pic.set_title('split=True')
    
    Out[73]:
    Text(0.5, 1.0, 'split=True')
     
    In [70]:
    ax = sns.violinplot(x="day", y="total_bill", hue="smoker",
                        data=tips, palette="muted", split=True)
    
     

    3.6 linewidth:箱体边框线的粗细

    In [74]:
    fig, ax =plt.subplots(1,3,constrained_layout=True, figsize=(8, 3))
    pic = sns.violinplot(x="day", y="total_bill", data=tips, ax=ax[0])
    pic.set_title('default linewidth')
    pic = sns.violinplot(x="day", y="total_bill", data=tips, ax=ax[1], linewidth=1)
    pic.set_title('linewidth=1')
    pic = sns.violinplot(x="day", y="total_bill", data=tips, ax=ax[2], linewidth=5)
    pic.set_title('linewidth=5')
    
    Out[74]:
    Text(0.5, 1.0, 'linewidth=5')
     

    3.7 width:设置箱体宽度

    In [81]:
    fig, ax =plt.subplots(1,2,constrained_layout=True, figsize=(12, 3))
    pic = sns.violinplot(x="day", y="total_bill", data=tips, ax=ax[0])
    pic.set_title('default width')
    pic = sns.violinplot(x="day", y="total_bill", data=tips, ax=ax[1], width=1)
    _ = pic.set_title('width=1')
    
     
  • 相关阅读:
    Linux中通过PID找到对应的进程以及所在目录
    好消息,完整版的jwt鉴权机制来了呦
    clean code-代码整洁之道 阅读笔记(第十二章)
    记一次生产大对象及GC时长优化经验
    前端用F11打开伪装更新页面demo后,老板被欺骗多次!
    【项目实战课】人人免费可学,基于ResNet的生活垃圾图像分类实战
    [C]二叉树的实现——喵喵成长记
    一文讲解Linux内核中的设计模式
    22. 概率与统计 - 贝叶斯统计&机器学习分类指标
    【LeetCode】移除盒子 [H](记忆化搜索)
  • 原文地址:https://www.cnblogs.com/chenhuabin/p/15925939.html