seaborn.
barplot
(*, x=None, y=None, hue=None, data=None, order=None, hue_order=None, estimator=<function mean at 0x7fecadf1cee0>, ci=95, n_boot=1000, units=None, seed=None, orient=None, color=None, palette=None, saturation=0.75, errcolor='.26', errwidth=None, capsize=None, dodge=True, ax=None, **kwargs)¶Show point estimates and confidence intervals as rectangular bars.
A bar plot represents an estimate of central tendency for a numeric variable with the height of each rectangle and provides some indication of the uncertainty around that estimate using error bars. Bar plots include 0 in the quantitative axis range, and they are a good choice when 0 is a meaningful value for the quantitative variable, and you want to make comparisons against it.
For datasets where 0 is not a meaningful value, a point plot will allow you to focus on differences between levels of one or more categorical variables.
It is also important to keep in mind that a bar plot shows only the mean (or other estimator) value, but in many cases it may be more informative to show the distribution of values at each level of the categorical variables. In that case, other approaches such as a box or violin plot may be more appropriate.
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 for more information.
data
or vector data, optionalInputs for plotting long-form data. See examples for interpretation.
Dataset for plotting. If x
and 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.
Statistical function to estimate within each categorical bin.
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.
Number of bootstrap iterations to use when computing confidence intervals.
data
or vector data, optionalIdentifier of sampling units, which will be used to perform a multilevel bootstrap and account for repeated measures design.
Seed or random number generator for reproducible bootstrapping.
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 x
and 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
spec.
Color for the lines that represent the confidence interval.
Thickness of error bar lines (and caps).
Width of the “caps” on error bars.
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
matplotlib.axes.Axes.bar()
.
Returns the Axes object with the plot drawn onto it.
See also
Examples
Draw a set of vertical bar plots grouped by a categorical variable:
>>> import seaborn as sns
>>> sns.set_theme(style="whitegrid")
>>> tips = sns.load_dataset("tips")
>>> ax = sns.barplot(x="day", y="total_bill", data=tips)
Draw a set of vertical bars with nested grouping by a two variables:
>>> ax = sns.barplot(x="day", y="total_bill", hue="sex", data=tips)
Draw a set of horizontal bars:
>>> ax = sns.barplot(x="tip", y="day", data=tips)
Control bar order by passing an explicit order:
>>> ax = sns.barplot(x="time", y="tip", data=tips,
... order=["Dinner", "Lunch"])
Use median as the estimate of central tendency:
>>> from numpy import median
>>> ax = sns.barplot(x="day", y="tip", data=tips, estimator=median)
Show the standard error of the mean with the error bars:
>>> ax = sns.barplot(x="day", y="tip", data=tips, ci=68)
Show standard deviation of observations instead of a confidence interval:
>>> ax = sns.barplot(x="day", y="tip", data=tips, ci="sd")
Add “caps” to the error bars:
>>> ax = sns.barplot(x="day", y="tip", data=tips, capsize=.2)
Use a different color palette for the bars:
>>> ax = sns.barplot(x="size", y="total_bill", data=tips,
... palette="Blues_d")
Use hue
without changing bar position or width:
>>> tips["weekend"] = tips["day"].isin(["Sat", "Sun"])
>>> ax = sns.barplot(x="day", y="total_bill", hue="weekend",
... data=tips, dodge=False)
Plot all bars in a single color:
>>> ax = sns.barplot(x="size", y="total_bill", data=tips,
... color="salmon", saturation=.5)
Use matplotlib.axes.Axes.bar()
parameters to control the style.
>>> ax = sns.barplot(x="day", y="total_bill", data=tips,
... linewidth=2.5, facecolor=(1, 1, 1, 0),
... errcolor=".2", edgecolor=".2")
Use catplot()
to combine a barplot()
and a FacetGrid
.
This allows grouping within additional categorical variables. Using
catplot()
is safer than using FacetGrid
directly, as it
ensures synchronization of variable order across facets:
>>> g = sns.catplot(x="sex", y="total_bill",
... hue="smoker", col="time",
... data=tips, kind="bar",
... height=4, aspect=.7);