20.md 8.7 KB
Newer Older
W
init  
wizardforcel 已提交
1 2 3 4 5 6
# seaborn.barplot

```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 已提交
7
条形图以矩形条的方式展示数据的点估值和置信区间
W
init  
wizardforcel 已提交
8

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

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

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

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

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

G
GKcmdr 已提交
22
大多数情况下,您可以使用numpy的对象或者python的对象,但是用pandas对象更好,因为相关的but pandas objects are preferable because the associated names will be used to annotate the axes. 另外,为了控制绘图元素 您也可以可以用分类类型来组合不同的变量。
W
init  
wizardforcel 已提交
23

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

W
init  
wizardforcel 已提交
26

G
GKcmdr 已提交
27
详情请看[教程](../tutorial/categorical.html#categorical-tutorial)
W
init  
wizardforcel 已提交
28 29 30

| Parameters: | **x, y, hue** : names of variables in `data` or vector data, optional

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

**data** : DataFrame, array, or list of arrays, optional

G
GKcmdr 已提交
35
> 用于绘图的数据集。如果缺失X值和Y值, 这个数据集会被解释成wide-form,否则this is interpreted as wide-form. Otherwise it is expected to be long-form.
W
init  
wizardforcel 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

**order, hue_order** : lists of strings, optional

> Order to plot the categorical levels in, otherwise the levels are inferred from the data objects.

**estimator** : callable that maps vector -&gt; scalar, optional

> Statistical function to estimate within each categorical bin.

**ci** : float or “sd” or None, optional

> Size of confidence intervals to draw around estimated values. If “sd”, skip bootstrapping and draw the standard deviation of the observations. If `None`, no bootstrapping will be performed, and error bars will not be drawn.

**n_boot** : int, optional

G
GKcmdr 已提交
51
> 计算置信区间需要的Boostrap迭代次数。
W
init  
wizardforcel 已提交
52 53 54 55 56 57 58 59 60 61 62

**units** : name of variable in `data` or vector data, optional

> Identifier of sampling units, which will be used to perform a multilevel bootstrap and account for repeated measures design.

**orient** : “v” &#124; “h”, optional

> Orientation of the plot (vertical or horizontal). This is usually inferred from the dtype of the input variables, but can be used to specify when the “categorical” variable is a numeric or when plotting wide-form data.

**color** : matplotlib color, optional

G
GKcmdr 已提交
63
> 作用于所有元素的颜色,或者渐变色的种子。
W
init  
wizardforcel 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

**palette** : palette name, list, or dict, optional

> Colors to use for the different levels of the `hue` variable. Should be something that can be interpreted by [`color_palette()`](seaborn.color_palette.html#seaborn.color_palette "seaborn.color_palette"), or a dictionary mapping hue levels to matplotlib colors.

**saturation** : float, optional

> 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.

**errcolor** : matplotlib color

> Color for the lines that represent the confidence interval.

**errwidth** : float, optional

G
GKcmdr 已提交
79
> 错误条的线的厚度。
W
init  
wizardforcel 已提交
80 81 82 83 84 85 86 87 88 89 90

**capsize** : float, optional

> Width of the “caps” on error bars.

**dodge** : bool, optional

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

**ax** : matplotlib Axes, optional

G
GKcmdr 已提交
91
> 指定一个Axes用于绘图,如果不指定,则使用当前的Axes。
W
init  
wizardforcel 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132

**kwargs** : key, value mappings

> Other keyword arguments are passed through to `plt.bar` at draw time.

 |
| --- | --- |
| Returns: | **ax** : matplotlib Axes

> Returns the Axes object with the plot drawn onto it.

 |
| --- | --- |

See also

Show the counts of observations in each categorical bin.Show point estimates and confidence intervals using scatterplot glyphs.Combine a categorical plot with a class:<cite>FacetGrid</cite>.

Examples

Draw a set of vertical bar plots grouped by a categorical variable:

```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)

Draw a set of vertical bars with nested grouping by a two variables:

```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 已提交
133
绘制一组水平柱状图:
W
init  
wizardforcel 已提交
134 135 136 137 138 139 140 141

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

```

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

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

```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 已提交
152
用中值来评估数据的集中趋势:
W
init  
wizardforcel 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170

```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)

Show the standard error of the mean with the error bars:

```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 已提交
171
展示数据的标准差:
W
init  
wizardforcel 已提交
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188

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

```

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

Add “caps” to the error bars:

```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 已提交
189
使用一个不同的调色盘来绘制图案:
W
init  
wizardforcel 已提交
190 191 192 193 194 195 196 197 198

```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 已提交
199
在不改变条柱的位置或者宽度的前提下,使用 `hue` :
W
init  
wizardforcel 已提交
200 201 202 203 204 205 206 207 208 209

```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 已提交
210
用同一种颜色绘制所有条柱:
W
init  
wizardforcel 已提交
211 212 213 214 215 216 217 218 219

```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 已提交
220
`plt.bar` 关键字参数进一步改变图表的样式:
W
init  
wizardforcel 已提交
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240

```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)

Use [`catplot()`](seaborn.catplot.html#seaborn.catplot "seaborn.catplot") to combine a [`barplot()`](#seaborn.barplot "seaborn.barplot") and a [`FacetGrid`](seaborn.FacetGrid.html#seaborn.FacetGrid "seaborn.FacetGrid"). This allows grouping within additional categorical variables. Using [`catplot()`](seaborn.catplot.html#seaborn.catplot "seaborn.catplot") is safer than using [`FacetGrid`](seaborn.FacetGrid.html#seaborn.FacetGrid "seaborn.FacetGrid") directly, as it ensures synchronization of variable order across facets:

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

```

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