21.md 5.5 KB
Newer Older
W
init  
wizardforcel 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
# seaborn.countplot

```py
seaborn.countplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, orient=None, color=None, palette=None, saturation=0.75, dodge=True, ax=None, **kwargs)
```

Show the counts of observations in each categorical bin using bars.

A count plot can be thought of as a histogram across a categorical, instead of quantitative, variable. The basic API and options are identical to those for [`barplot()`](seaborn.barplot.html#seaborn.barplot "seaborn.barplot"), so you can compare counts across nested variables.

Input data can be passed in a variety of formats, including:

*   Vectors of data represented as lists, numpy arrays, or pandas Series objects passed directly to the `x`, `y`, and/or `hue` parameters.
*   A “long-form” DataFrame, in which case the `x`, `y`, and `hue` variables will determine how the data are plotted.
*   A “wide-form” DataFrame, such that each numeric column will be plotted.
*   An array or list of vectors.

In most cases, it is possible to use numpy or Python objects, but pandas objects are preferable because the associated names will be used to annotate the axes. Additionally, you can use Categorical types for the grouping variables to control the order of plot elements.

This function always treats one of the variables as categorical and draws data at ordinal positions (0, 1, … n) on the relevant axis, even when the data has a numeric or date type.

See the [tutorial](../tutorial/categorical.html#categorical-tutorial) for more information.

W
wizardforcel 已提交
24
参数:`x, y, hue`:names of variables in `data` or vector data, optional
W
init  
wizardforcel 已提交
25 26 27

> Inputs for plotting long-form data. See examples for interpretation.

W
wizardforcel 已提交
28
`data`:DataFrame, array, or list of arrays, optional
W
init  
wizardforcel 已提交
29 30 31

> Dataset for plotting. If `x` and `y` are absent, this is interpreted as wide-form. Otherwise it is expected to be long-form.

W
wizardforcel 已提交
32
`order, hue_order`:lists of strings, optional
W
init  
wizardforcel 已提交
33 34 35

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

W
wizardforcel 已提交
36
`orient`:“v” | “h”, optional
W
init  
wizardforcel 已提交
37 38 39

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

W
wizardforcel 已提交
40
`color`:matplotlib color, optional
W
init  
wizardforcel 已提交
41 42 43

> Color for all of the elements, or seed for a gradient palette.

W
wizardforcel 已提交
44
`palette`:palette name, list, or dict, optional
W
init  
wizardforcel 已提交
45 46 47

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

W
wizardforcel 已提交
48
`saturation`:float, optional
W
init  
wizardforcel 已提交
49 50 51

> 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
wizardforcel 已提交
52
`dodge`:bool, optional
W
init  
wizardforcel 已提交
53 54 55

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

W
wizardforcel 已提交
56
`ax`:matplotlib Axes, optional
W
init  
wizardforcel 已提交
57 58 59

> Axes object to draw the plot onto, otherwise uses the current Axes.

W
wizardforcel 已提交
60
`kwargs`:key, value mappings
W
init  
wizardforcel 已提交
61 62 63

> Other keyword arguments are passed to `plt.bar`.

W
wizardforcel 已提交
64 65

返回值:`ax`:matplotlib Axes
W
init  
wizardforcel 已提交
66 67 68

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

W
wizardforcel 已提交
69

W
init  
wizardforcel 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 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 133 134 135 136 137

See also

Show point estimates and confidence intervals using bars.Combine a categorical plot with a class:<cite>FacetGrid</cite>.

Examples

Show value counts for a single categorical variable:

```py
>>> import seaborn as sns
>>> sns.set(style="darkgrid")
>>> titanic = sns.load_dataset("titanic")
>>> ax = sns.countplot(x="class", data=titanic)

```

![http://seaborn.pydata.org/_images/seaborn-countplot-1.png](img/840a3b4206930d48fae0f0d4549b87a0.jpg)

Show value counts for two categorical variables:

```py
>>> ax = sns.countplot(x="class", hue="who", data=titanic)

```

![http://seaborn.pydata.org/_images/seaborn-countplot-2.png](img/7fde400b120a0b6cd6aca0cb2de690db.jpg)

Plot the bars horizontally:

```py
>>> ax = sns.countplot(y="class", hue="who", data=titanic)

```

![http://seaborn.pydata.org/_images/seaborn-countplot-3.png](img/512afaa1b09caf55faf909a34995137a.jpg)

Use a different color palette:

```py
>>> ax = sns.countplot(x="who", data=titanic, palette="Set3")

```

![http://seaborn.pydata.org/_images/seaborn-countplot-4.png](img/d3ac1e2e1f590489ef76341664562f87.jpg)

Use `plt.bar` keyword arguments for a different look:

```py
>>> ax = sns.countplot(x="who", data=titanic,
...                    facecolor=(0, 0, 0, 0),
...                    linewidth=5,
...                    edgecolor=sns.color_palette("dark", 3))

```

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

Use [`catplot()`](seaborn.catplot.html#seaborn.catplot "seaborn.catplot") to combine a [`countplot()`](#seaborn.countplot "seaborn.countplot") 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="class", hue="who", col="survived",
...                 data=titanic, kind="count",
...                 height=4, aspect=.7);

```

![http://seaborn.pydata.org/_images/seaborn-countplot-6.png](img/5d75285f3c08542cd9db2296ef737d54.jpg)