18.md 7.5 KB
Newer Older
W
init  
wizardforcel 已提交
1 2
# seaborn.boxenplot

W
credit  
wizardforcel 已提交
3 4
> 译者:[FindNorthStar](https://github.com/FindNorthStar)

W
init  
wizardforcel 已提交
5 6 7 8
```py
seaborn.boxenplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, orient=None, color=None, palette=None, saturation=0.75, width=0.8, dodge=True, k_depth='proportion', linewidth=None, scale='exponential', outlier_prop=None, ax=None, **kwargs)
```

F
findns 已提交
9
为更大的数据集绘制增强的箱型图。
W
init  
wizardforcel 已提交
10

F
findns 已提交
11
这种风格的绘图最初被命名为“信值图”,因为它显示了大量被定义为“置信区间”的分位数。它类似于绘制分布的非参数表示的箱形图,其中所有特征对应于实际观察的数值点。通过绘制更多分位数,它提供了有关分布形状的更多信息,特别是尾部数据的分布。欲了解更详细的解释,您可以阅读介绍该绘图的论文:
W
init  
wizardforcel 已提交
12 13 14

[https://vita.had.co.nz/papers/letter-value-plot.html](https://vita.had.co.nz/papers/letter-value-plot.html)

F
findns 已提交
15
输入数据可以通过多种格式传入,包括:
W
init  
wizardforcel 已提交
16

F
findns 已提交
17 18 19 20
*   格式为列表,numpy数组或pandas Series对象的数据向量可以直接传递给`x``y``hue`参数。
*   对于长格式的DataFrame,`x``y`,和`hue`参数会决定如何绘制数据。
*   对于宽格式的DataFrame,每一列数值列都会被绘制。
*   一个数组或向量的列表。
W
init  
wizardforcel 已提交
21

F
findns 已提交
22
在大多数情况下,可以使用numpy或Python对象,但更推荐使用pandas对象,因为与数据关联的列名/行名可以用于标注横轴/纵轴的名称。此外,您可以使用分类类型对变量进行分组以控制绘图元素的顺序。
W
init  
wizardforcel 已提交
23

F
findns 已提交
24
此函数始终将其中一个变量视为分类,并在相关轴上的序数位置(0,1,... n)处绘制数据,即使数据属于数值类型或日期类型也是如此。
W
init  
wizardforcel 已提交
25

F
findns 已提交
26
更多信息请参阅 [教程](../tutorial/categorical.html#categorical-tutorial)
W
init  
wizardforcel 已提交
27

F
findns 已提交
28
参数:`x, y, hue``data`或向量数据中的变量名称,可选
W
init  
wizardforcel 已提交
29

F
findns 已提交
30
> 用于绘制长格式数据的输入。查看样例以进一步理解。
W
init  
wizardforcel 已提交
31

F
findns 已提交
32
`data`:DataFrame,数组,数组列表,可选
W
init  
wizardforcel 已提交
33

F
findns 已提交
34
> 用于绘图的数据集。如果`x`和`y`都缺失,那么数据将被视为宽格式。否则数据被视为长格式。
W
init  
wizardforcel 已提交
35

F
findns 已提交
36
`order, hue_order`:字符串列表,可选
W
init  
wizardforcel 已提交
37

F
findns 已提交
38
> 控制分类变量(对应的条形图)的绘制顺序,若缺失则从数据中推断分类变量的顺序。
W
init  
wizardforcel 已提交
39

F
findns 已提交
40
`orient`:“v” | “h”,可选
W
init  
wizardforcel 已提交
41

F
findns 已提交
42
> 控制绘图的方向(垂直或水平)。这通常是从输入变量的dtype推断出来的,但是当“分类”变量为数值型或绘制宽格式数据时可用于指定绘图的方向。
W
init  
wizardforcel 已提交
43

F
findns 已提交
44
`color`:matplotlib颜色,可选
W
init  
wizardforcel 已提交
45

F
findns 已提交
46
> 所有元素的颜色,或渐变调色板的种子颜色。
W
init  
wizardforcel 已提交
47

F
findns 已提交
48
`palette`:调色板名称,列表或字典,可选
W
init  
wizardforcel 已提交
49

F
findns 已提交
50
> 用于`hue`变量的不同级别的颜色。可以从 [`color_palette()`](seaborn.color_palette.html#seaborn.color_palette "seaborn.color_palette") 得到一些解释,或者将色调级别映射到matplotlib颜色的字典。
W
init  
wizardforcel 已提交
51

F
findns 已提交
52
`saturation`:float,可选
W
init  
wizardforcel 已提交
53

F
findns 已提交
54
> 控制用于绘制颜色的原始饱和度的比例。通常大幅填充在轻微不饱和的颜色下看起来更好,如果您希望绘图颜色与输入颜色规格完美匹配可将其设置为`1`。
W
init  
wizardforcel 已提交
55

F
findns 已提交
56
`width`:float,可选
W
init  
wizardforcel 已提交
57

F
findns 已提交
58
> 不使用色调嵌套时完整元素的宽度,或主要分组变量一个级别的所有元素的宽度。
W
init  
wizardforcel 已提交
59

F
findns 已提交
60
`dodge`:bool,可选
W
init  
wizardforcel 已提交
61

F
findns 已提交
62
> 使用色调嵌套时,元素是否应沿分类轴移动。
W
init  
wizardforcel 已提交
63

F
findns 已提交
64
`k_depth`:“proportion” | “tukey” | “trustworthy”,可选
W
init  
wizardforcel 已提交
65

F
findns 已提交
66
> 通过增大百分比的粒度控制绘制的盒形图数目。所有方法都在Wickham的论文中有详细描述。每个参数代表利用不同的统计特性对异常值的数量做出不同的假设。
W
init  
wizardforcel 已提交
67

F
findns 已提交
68
`linewidth`:float,可选
W
init  
wizardforcel 已提交
69

F
findns 已提交
70
> 构图元素的灰线宽度。
W
init  
wizardforcel 已提交
71

W
wizardforcel 已提交
72
`scale`:“linear” | “exponential” | “area”
W
init  
wizardforcel 已提交
73

F
findns 已提交
74
> 用于控制增强箱型图宽度的方法。所有参数都会给显示效果造成影响。 “linear” 通过恒定的线性因子减小宽度,“exponential” 使用未覆盖的数据的比例调整宽度, “area” 与所覆盖的数据的百分比成比例。
W
init  
wizardforcel 已提交
75

F
findns 已提交
76
`outlier_prop`:float,可选
W
init  
wizardforcel 已提交
77

F
findns 已提交
78
> 被认为是异常值的数据比例。与 `k_depth` 结合使用以确定要绘制的百分位数。默认值为0.007作为异常值的比例。该参数取值应在[0,1]范围内。
W
init  
wizardforcel 已提交
79

F
findns 已提交
80
`ax`:matplotlib轴,可选
W
init  
wizardforcel 已提交
81

F
findns 已提交
82
> 绘图时使用的Axes轴对象,否则使用当前Axes轴对象。
W
init  
wizardforcel 已提交
83

F
findns 已提交
84
`kwargs`:键,值映射
W
init  
wizardforcel 已提交
85

F
findns 已提交
86
> 其他在绘制时传递给`plt.plot`和`plt.scatter`参数。
W
init  
wizardforcel 已提交
87 88


F
findns 已提交
89
返回值:`ax`:matplotlib轴
W
init  
wizardforcel 已提交
90

F
findns 已提交
91
> 返回Axes对轴象,并在其上绘制绘图。
W
init  
wizardforcel 已提交
92 93 94



F
findns 已提交
95
亦可参见
W
init  
wizardforcel 已提交
96

F
findns 已提交
97
boxplot和核密度估计的结合。一个传统的箱型图具有类似的API。
W
init  
wizardforcel 已提交
98

F
findns 已提交
99
示例
W
init  
wizardforcel 已提交
100

F
findns 已提交
101
绘制一个独立的横向增强箱型图:
W
init  
wizardforcel 已提交
102 103 104 105 106 107 108 109 110 111 112

```py
>>> import seaborn as sns
>>> sns.set(style="whitegrid")
>>> tips = sns.load_dataset("tips")
>>> ax = sns.boxenplot(x=tips["total_bill"])

```

![http://seaborn.pydata.org/_images/seaborn-boxenplot-1.png](img/ea7362d005109093ddfe7d7a0039a13e.jpg)

F
findns 已提交
113
根据分类变量分组绘制一个纵向的增强箱型图:
W
init  
wizardforcel 已提交
114 115 116 117 118 119 120 121

```py
>>> ax = sns.boxenplot(x="day", y="total_bill", data=tips)

```

![http://seaborn.pydata.org/_images/seaborn-boxenplot-2.png](img/31c79f0cf22d453e10799da960e3e801.jpg)

F
findns 已提交
122
根据2个分类变量嵌套分组绘制一个增强箱型图:
W
init  
wizardforcel 已提交
123 124 125 126 127 128 129 130 131

```py
>>> ax = sns.boxenplot(x="day", y="total_bill", hue="smoker",
...                    data=tips, palette="Set3")

```

![http://seaborn.pydata.org/_images/seaborn-boxenplot-3.png](img/1db91ed8446afc825fa5bba21f1ef278.jpg)

F
findns 已提交
132
当一些数据为空时根据嵌套分组绘制一个增强箱型图:
W
init  
wizardforcel 已提交
133 134 135 136 137 138 139 140 141

```py
>>> ax = sns.boxenplot(x="day", y="total_bill", hue="time",
...                    data=tips, linewidth=2.5)

```

![http://seaborn.pydata.org/_images/seaborn-boxenplot-4.png](img/2e57af8b26439c244046ff7846601335.jpg)

F
findns 已提交
142
通过显式传入参数指定顺序控制箱型图的显示顺序:
W
init  
wizardforcel 已提交
143 144 145 146 147 148 149 150 151

```py
>>> ax = sns.boxenplot(x="time", y="tip", data=tips,
...                    order=["Dinner", "Lunch"])

```

![http://seaborn.pydata.org/_images/seaborn-boxenplot-5.png](img/e8fa81696195ce058546e429317075bc.jpg)

F
findns 已提交
152
针对DataFrame里每一个数值型变量绘制增强箱型图:
W
init  
wizardforcel 已提交
153 154 155 156 157 158 159 160 161

```py
>>> iris = sns.load_dataset("iris")
>>> ax = sns.boxenplot(data=iris, orient="h", palette="Set2")

```

![http://seaborn.pydata.org/_images/seaborn-boxenplot-6.png](img/a9e939280daed8ec0712c8e6e6be78fb.jpg)

F
findns 已提交
162
使用 [`stripplot()`](seaborn.stripplot.html#seaborn.stripplot "seaborn.stripplot") 显示箱型图顶部的数据点:
W
init  
wizardforcel 已提交
163 164 165 166 167 168 169 170 171 172

```py
>>> ax = sns.boxenplot(x="day", y="total_bill", data=tips)
>>> ax = sns.stripplot(x="day", y="total_bill", data=tips,
...                    size=4, jitter=True, color="gray")

```

![http://seaborn.pydata.org/_images/seaborn-boxenplot-7.png](img/fb3de8051b91bb0be2143717f96c0a7c.jpg)

F
findns 已提交
173
[`catplot()`](seaborn.catplot.html#seaborn.catplot "seaborn.catplot") to combine [`boxenplot()`](#seaborn.boxenplot "seaborn.boxenplot") 以及 [`FacetGrid`](seaborn.FacetGrid.html#seaborn.FacetGrid "seaborn.FacetGrid") 结合起来使用。这允许您通过额外的分类变量进行分组。使用 [`catplot()`](seaborn.catplot.html#seaborn.catplot "seaborn.catplot") 比直接使用 [`FacetGrid`](seaborn.FacetGrid.html#seaborn.FacetGrid "seaborn.FacetGrid") 更为安全,因为它保证了不同切面上变量同步的顺序:
W
init  
wizardforcel 已提交
174 175 176 177 178 179 180 181 182

```py
>>> g = sns.catplot(x="sex", y="total_bill",
...                 hue="smoker", col="time",
...                 data=tips, kind="boxen",
...                 height=4, aspect=.7);

```

M
melon-bun 已提交
183
![http://seaborn.pydata.org/_images/seaborn-boxenplot-8.png](img/ef97d95b2084af9b8636c2514545289d.jpg)