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
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
A “long-form” DataFrame, in which case the
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 for more information.
dataor vector data, optional
Inputs for plotting long-form data. See examples for interpretation.
Dataset for plotting. If
y are absent, this is
interpreted as wide-form. Otherwise it is expected to be long-form.
Order to plot the categorical levels in, otherwise the levels are inferred from the data objects.
Orientation of the plot (vertical or horizontal). This is usually
inferred based on the type of the input variables, but it can be used
to resolve ambiguitiy when both
y are numeric or when
plotting wide-form data.
Color for all of the elements, or seed for a gradient palette.
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.
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
When hue nesting is used, whether elements should be shifted along the categorical axis.
Axes object to draw the plot onto, otherwise uses the current Axes.
Other keyword arguments are passed through to
Returns the Axes object with the plot drawn onto it.
Show value counts for a single categorical variable:
>>> import seaborn as sns >>> sns.set_theme(style="darkgrid") >>> titanic = sns.load_dataset("titanic") >>> ax = sns.countplot(x="class", data=titanic)
Show value counts for two categorical variables:
>>> ax = sns.countplot(x="class", hue="who", data=titanic)
Plot the bars horizontally:
>>> ax = sns.countplot(y="class", hue="who", data=titanic)
Use a different color palette:
>>> ax = sns.countplot(x="who", data=titanic, palette="Set3")
matplotlib.axes.Axes.bar() parameters to control the style.
>>> ax = sns.countplot(x="who", data=titanic, ... facecolor=(0, 0, 0, 0), ... linewidth=5, ... edgecolor=sns.color_palette("dark", 3))
catplot() to combine a
countplot() and a
FacetGrid. This allows grouping within additional categorical
catplot() is safer than using
directly, as it ensures synchronization of variable order across facets:
>>> g = sns.catplot(x="class", hue="who", col="survived", ... data=titanic, kind="count", ... height=4, aspect=.7);