20.md 8.4 KB
Newer Older
W
init  
wizardforcel 已提交
1 2
# seaborn.barplot

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

W
init  
wizardforcel 已提交
5 6 7 8
```py
seaborn.barplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, estimator=<function mean>, ci=95, n_boot=1000, units=None, orient=None, color=None, palette=None, saturation=0.75, errcolor='.26', errwidth=None, capsize=None, dodge=True, ax=None, **kwargs)
```

G
GKcmdr 已提交
9
条形图以矩形条的方式展示数据的点估值和置信区间
W
init  
wizardforcel 已提交
10

W
wizardforcel 已提交
11
条形图用每个矩形的高度来表示数值变量的集中趋势的估计值,并提供误差条来显示估计值得不确定度。条形图的纵轴是从零开始的,这对于 0 值是有意义的情况是非常好的。
W
init  
wizardforcel 已提交
12

W
wizardforcel 已提交
13
对于数据集中的 0 值没有实际意义的情况,散点图可以让您专注于一个或多个分类变量之间的差异。
W
init  
wizardforcel 已提交
14

G
GKcmdr 已提交
15
要注意的是,条形图只显示平均值(或者其他的估计值),但是在大部分情况下,展示数值在不同分类变量上的分布会更有用。如果要表示出数据的分布,用箱型图或者小提琴图会更恰当。
W
init  
wizardforcel 已提交
16

G
GKcmdr 已提交
17
输入数据的格式可以不同,包括:
W
init  
wizardforcel 已提交
18

W
wizardforcel 已提交
19 20
*   以列表,numpy array 或者 pandas 中的 Series object 表示的向量。这些向量可以直接传入 `x`, `y`, 以及 `hue` 参数。
*   长表, x 值,y 值和色相变量决定了数据是如何绘制的。
G
GKcmdr 已提交
21
*   宽表,每个列的数值都会被绘制出来.
G
GKcmdr 已提交
22
*   数组或者列表的向量。
W
init  
wizardforcel 已提交
23

W
wizardforcel 已提交
24
大多数情况下,您可以使用 numpy 的对象或者 python 的对象,但是用 pandas 对象更好,因为相关的列名会被标注在图标上。 另外,为了控制绘图元素 您也可以可以用分类类型来组合不同的变量。
W
init  
wizardforcel 已提交
25

G
GKcmdr 已提交
26 27
这个函数始终把一个变量当做分类变量,即使这个数据是数值类型或者日期类型,并且按照序数顺序绘制在相关的轴上。

沈宇 已提交
28
详情请看[教程](http://seaborn.pydata.org/tutorial/categorical.html#categorical-tutorial)
W
init  
wizardforcel 已提交
29

飞龙 已提交
30
参数:`x, y, hue``data` 中的变量名词或者向量, optional
W
init  
wizardforcel 已提交
31

W
wizardforcel 已提交
32
> 用于绘制 long-form 数据的变量名称.
W
init  
wizardforcel 已提交
33

飞龙 已提交
34
`data`:DataFrame, 数组, 数组列表, optional
W
init  
wizardforcel 已提交
35

W
wizardforcel 已提交
36
> 用于绘图的数据集。如果数据集有 x 和 y,数据集会被认为是 long-form,否则会被认为是 wide-form。
W
init  
wizardforcel 已提交
37

飞龙 已提交
38
`order, hue_order`:字符串列表, optional
W
init  
wizardforcel 已提交
39

G
GKcmdr 已提交
40
> 绘制类别变量的顺序,如果没有,则从数据对象中推断绘图顺序。
W
init  
wizardforcel 已提交
41

飞龙 已提交
42
`estimator`:映射向量 -&gt; 标量, optional
W
init  
wizardforcel 已提交
43

G
GKcmdr 已提交
44
> 统计函数用于估计每个分类纸条中的值。.
W
init  
wizardforcel 已提交
45

W
wizardforcel 已提交
46
`ci`:float or “sd” or None, optional
W
init  
wizardforcel 已提交
47

W
wizardforcel 已提交
48
> 估计值周围的置信区间大小。如果输入的是 “sd”(标准差),会跳过 bootstrapping 的过程,只绘制数据的标准差。 如果输入是的是`None`, 不会执行 botstrapping,而且错误条也不会绘制。
W
init  
wizardforcel 已提交
49

W
wizardforcel 已提交
50
`n_boot`:int, optional
W
init  
wizardforcel 已提交
51

W
wizardforcel 已提交
52
> 计算置信区间需要的 Boostrap 迭代次数。
W
init  
wizardforcel 已提交
53

W
wizardforcel 已提交
54
`units`:name of variable in `data` or vector data, optional
W
init  
wizardforcel 已提交
55

W
wizardforcel 已提交
56
> 采样单元的标识符,用于执行多级 bootstrap 并解释重复测量设计。
W
init  
wizardforcel 已提交
57

W
wizardforcel 已提交
58
`orient`:“v” &#124; “h”, optional
W
init  
wizardforcel 已提交
59

G
GKcmdr 已提交
60
> 绘图的方向(垂直或水平)。这通常是从输入变量的数据类型推断出来的,但是可以用来指定“分类”变量是数字还是宽格式数据。
W
init  
wizardforcel 已提交
61

W
wizardforcel 已提交
62
`color`:matplotlib color, optional
W
init  
wizardforcel 已提交
63

G
GKcmdr 已提交
64
> 作用于所有元素的颜色,或者渐变色的种子。
W
init  
wizardforcel 已提交
65

W
wizardforcel 已提交
66
`palette`:palette name, list, or dict, optional
W
init  
wizardforcel 已提交
67

W
wizardforcel 已提交
68
> 不同级别的 `hue` 变量的颜色。 颜色要能被 [`color_palette()`]解释(seaborn.color_palette.html#seaborn.color_palette "seaborn.color_palette"), 或者一个能映射到 matplotlib 颜色的字典。
W
init  
wizardforcel 已提交
69

W
wizardforcel 已提交
70
`saturation`:float, optional
W
init  
wizardforcel 已提交
71

G
GKcmdr 已提交
72
>  Proportion of the original saturation to draw colors at. Large patches often look better with slightly desaturated colors, but set this to `1` if you want the plot colors to perfectly match the input color spec.
W
init  
wizardforcel 已提交
73

W
wizardforcel 已提交
74
`errcolor`:matplotlib color
W
init  
wizardforcel 已提交
75

G
GKcmdr 已提交
76
> 表示置信区间的线的颜色。
W
init  
wizardforcel 已提交
77

W
wizardforcel 已提交
78
`errwidth`:float, optional
W
init  
wizardforcel 已提交
79

G
GKcmdr 已提交
80
> 误差条的线的厚度。
W
init  
wizardforcel 已提交
81

W
wizardforcel 已提交
82
`capsize`:float, optional
W
init  
wizardforcel 已提交
83

G
GKcmdr 已提交
84
> 误差条端部的宽度。
W
init  
wizardforcel 已提交
85

G
GKcmdr 已提交
86
**dodge** : 布尔型, optional
W
init  
wizardforcel 已提交
87 88 89

> When hue nesting is used, whether elements should be shifted along the categorical axis.

W
wizardforcel 已提交
90
`ax`:matplotlib Axes, optional
W
init  
wizardforcel 已提交
91

W
wizardforcel 已提交
92
> 指定一个 Axes 用于绘图,如果不指定,则使用当前的 Axes。
W
init  
wizardforcel 已提交
93

W
wizardforcel 已提交
94
`kwargs`:key, value mappings
W
init  
wizardforcel 已提交
95

G
GKcmdr 已提交
96
> 其他的关键词参数在绘图时通过 `plt.bar` 传入。
W
init  
wizardforcel 已提交
97

W
wizardforcel 已提交
98
返回值:`ax`:matplotlib Axes
W
init  
wizardforcel 已提交
99

W
wizardforcel 已提交
100
> 返回有图表绘制的 Axes 对象。
W
init  
wizardforcel 已提交
101

W
wizardforcel 已提交
102

W
init  
wizardforcel 已提交
103 104 105

See also

W
wizardforcel 已提交
106
显示每个分类 bin 中的观察计数。使用散点图图示符显示点估计和置信区间。将分类图与类相结合:<cite>FacetGrid</cite>.
W
init  
wizardforcel 已提交
107 108 109

Examples

G
GKcmdr 已提交
110
绘制一组按类别变量分组的垂直条形图:
W
init  
wizardforcel 已提交
111 112 113 114 115 116 117 118 119 120 121

```py
>>> import seaborn as sns
>>> sns.set(style="whitegrid")
>>> tips = sns.load_dataset("tips")
>>> ax = sns.barplot(x="day", y="total_bill", data=tips)

```

![http://seaborn.pydata.org/_images/seaborn-barplot-1.png](img/9d1addc98b6a35ef0376219c56e7b7fd.jpg)

G
GKcmdr 已提交
122
绘制一组由两个变量嵌套分组的垂直条形图:
W
init  
wizardforcel 已提交
123 124 125 126 127 128 129 130

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

```

![http://seaborn.pydata.org/_images/seaborn-barplot-2.png](img/863249efe2403afa4fae2f2b6884d3bd.jpg)

G
GKcmdr 已提交
131
绘制一组水平条形图:
W
init  
wizardforcel 已提交
132 133 134 135 136 137 138 139

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

```

![http://seaborn.pydata.org/_images/seaborn-barplot-3.png](img/c3ea6265eaff0a4bfaec2966088cb66f.jpg)

G
GKcmdr 已提交
140
通过传入一个显式的顺序来控制条柱的顺序:
W
init  
wizardforcel 已提交
141 142 143 144 145 146 147 148 149

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

```

![http://seaborn.pydata.org/_images/seaborn-barplot-4.png](img/9233554272a5e436f6ab85c97a65010c.jpg)

G
GKcmdr 已提交
150
用中值来评估数据的集中趋势:
W
init  
wizardforcel 已提交
151 152 153 154 155 156 157 158 159

```py
>>> from numpy import median
>>> ax = sns.barplot(x="day", y="tip", data=tips, estimator=median)

```

![http://seaborn.pydata.org/_images/seaborn-barplot-5.png](img/2622373fb99932aa42e45c3b151135be.jpg)

G
GKcmdr 已提交
160
用误差条显示平均值的标准误差:
W
init  
wizardforcel 已提交
161 162 163 164 165 166 167 168

```py
>>> ax = sns.barplot(x="day", y="tip", data=tips, ci=68)

```

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

G
GKcmdr 已提交
169
展示数据的标准差:
W
init  
wizardforcel 已提交
170 171 172 173 174 175 176 177

```py
>>> ax = sns.barplot(x="day", y="tip", data=tips, ci="sd")

```

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

G
GKcmdr 已提交
178
给误差条增加“端点”:
W
init  
wizardforcel 已提交
179 180 181 182 183 184 185 186

```py
>>> ax = sns.barplot(x="day", y="tip", data=tips, capsize=.2)

```

![http://seaborn.pydata.org/_images/seaborn-barplot-8.png](img/5a69e1058d9b8b4b5be6dc15d1bad130.jpg)

G
GKcmdr 已提交
187
使用一个不同的调色盘来绘制图案:
W
init  
wizardforcel 已提交
188 189 190 191 192 193 194 195 196

```py
>>> ax = sns.barplot("size", y="total_bill", data=tips,
...                  palette="Blues_d")

```

![http://seaborn.pydata.org/_images/seaborn-barplot-9.png](img/ef011fca38d3c55dde21ee8363e93e61.jpg)

G
GKcmdr 已提交
197
在不改变条柱的位置或者宽度的前提下,使用 `hue` :
W
init  
wizardforcel 已提交
198 199 200 201 202 203 204 205 206 207

```py
>>> tips["weekend"] = tips["day"].isin(["Sat", "Sun"])
>>> ax = sns.barplot(x="day", y="total_bill", hue="weekend",
...                  data=tips, dodge=False)

```

![http://seaborn.pydata.org/_images/seaborn-barplot-10.png](img/d38d4ad12b16322a5ed00690bcbcd8b6.jpg)

G
GKcmdr 已提交
208
用同一种颜色绘制所有条柱:
W
init  
wizardforcel 已提交
209 210 211 212 213 214 215 216 217

```py
>>> ax = sns.barplot("size", y="total_bill", data=tips,
...                  color="salmon", saturation=.5)

```

![http://seaborn.pydata.org/_images/seaborn-barplot-11.png](img/4922c693b75b7656b2f16f8fd2dd6509.jpg)

G
GKcmdr 已提交
218
`plt.bar` 关键字参数进一步改变图表的样式:
W
init  
wizardforcel 已提交
219 220 221 222 223 224 225 226 227 228

```py
>>> ax = sns.barplot("day", "total_bill", data=tips,
...                  linewidth=2.5, facecolor=(1, 1, 1, 0),
...                  errcolor=".2", edgecolor=".2")

```

![http://seaborn.pydata.org/_images/seaborn-barplot-12.png](img/20114eb58ca40a3ccf0b20f14f426c83.jpg)

W
wizardforcel 已提交
229
使用 [`catplot()`](seaborn.catplot.html#seaborn.catplot "seaborn.catplot") 来结合 [`barplot()`](#seaborn.barplot "seaborn.barplot")[`FacetGrid`](seaborn.FacetGrid.html#seaborn.FacetGrid "seaborn.FacetGrid"). 这允许数据根据额外的类别变量分组。使用 [`catplot()`](seaborn.catplot.html#seaborn.catplot "seaborn.catplot") 比直接使用 [`FacetGrid`](seaborn.FacetGrid.html#seaborn.FacetGrid "seaborn.FacetGrid") 更安全, 因为它可以确保变量在不同的 facet 之间保持同步:
W
init  
wizardforcel 已提交
230 231 232 233 234 235 236 237 238

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

```

G
GKcmdr 已提交
239
![http://seaborn.pydata.org/_images/seaborn-barplot-13.png](img/a2d8b9c6867b1006b56e5508d5472c86.jpg)