seaborn.countplot

seaborn.countplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, orient=None, color=None, palette=None, saturation=0.75, 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(), 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.
  • Anything accepted by plt.boxplot (e.g. a 2d 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.

Parameters:

x, y, hue : names of variables in data or vector data, optional

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

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

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

order, hue_order : lists of strings, optional

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

orient : “v” | “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

Color for all of the elements, or seed for light_palette() when using hue nesting.

palette : seaborn color palette or dict, optional

Colors to use for the different levels of the hue variable. Should be something that can be interpreted by 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.

ax : matplotlib Axes, optional

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

kwargs : key, value mappings

Other keyword arguments are passed to plt.bar.

Returns:

ax : matplotlib Axes

Returns the Axes object with the boxplot drawn onto it.

See also

barplot
Show point estimates and confidence intervals using bars.
factorplot
Combine categorical plots and a class:FacetGrid.

Examples

Show value counts for a single categorical variable:

>>> import seaborn as sns
>>> sns.set(style="darkgrid")
>>> titanic = sns.load_dataset("titanic")
>>> ax = sns.countplot(x="class", data=titanic)
../_images/seaborn-countplot-1.png

Show value counts for two categorical variables:

>>> ax = sns.countplot(x="class", hue="who", data=titanic)
../_images/seaborn-countplot-2.png

Plot the bars horizontally:

>>> ax = sns.countplot(y="class", hue="who", data=titanic)
../_images/seaborn-countplot-3.png

Use a different color palette:

>>> ax = sns.countplot(x="who", data=titanic, palette="Set3")
../_images/seaborn-countplot-4.png

Use plt.bar keyword arguments for a different look:

>>> ax = sns.countplot(x="who", data=titanic,
...                    facecolor=(0, 0, 0, 0),
...                    linewidth=5,
...                    edgecolor=sns.color_palette("dark", 3))
../_images/seaborn-countplot-5.png