Categorical distribution comparison
Compare a numeric measurement across named categories such as days, groups, or classes.
Seaborn · Function
Draws box-and-whisker plots that compare quantitative distributions across categorical levels. Quartiles form the box, whiskers follow the configured whis rule, and outlying observations are shown separately.
Call boxplot with a dataset and categorical and numeric variables to compare distributions. It draws quartile-based boxes, extends whiskers according to the whis rule, and marks observations outside that rule as fliers; optional hue, ordering, orientation, scaling, and styling arguments refine the display.
seaborn.boxplot(*data=None, *, x=None, y=None, hue=None, order=None, hue_order=None, orient=None, color=None, palette=None, saturation=0.75, fill=True, dodge='auto', width=0.8, gap=0, whis=1.5, linecolor='auto', linewidth=None, fliersize=None, hue_norm=None, native_scale=False, log_scale=None, formatter=None, legend='auto', ax=None, **kwargs)| Name | Type | Description |
|---|---|---|
data | DataFrame, Series, dict, array, or list of arrays | Dataset for plotting. |
x | vector or key in data | Input data for the x axis. |
y | vector or key in data | Input data for the y axis. |
hue | vector or key in data | Semantic variable that is mapped to determine the color of plot elements. |
order | list of strings | Order to plot the categorical levels in. |
hue_order | list of strings | Specific order for the appearance of the hue levels. |
orient | “v” | “h” | “x” | “y” | Orientation of the plot. |
color | matplotlib color | Color for all of the elements. |
palette | palette name, list, or dict | Colors to use for the different levels of the hue variable. |
saturation | float | Proportion of the original saturation to draw colors at. |
fill | bool | Whether the boxes should be drawn with a solid fill. |
dodge | bool or “auto” | When hue nesting is used, whether the elements should be shifted along the categorical axis. |
matplotlib.axes.Axes
boxplot when comparing a quantitative distribution across categorical levels.data argument accepts common tabular or array-like dataset forms.native_scale=True to avoid ordinal repositioning when category values have a meaningful native scale.native_scale=True when the categorical variable's numeric or datetime scale should be preserved.import seaborn as sns
import matplotlib.pyplot as plt
sns.set_theme(style="whitegrid")
tips = sns.load_dataset("tips")
sns.boxplot(data=tips, x="day", y="total_bill", hue="smoker")
plt.show()This groups numeric bill totals by day and adds smoker as a nested categorical grouping.
Compare a numeric measurement across named categories such as days, groups, or classes.
Add hue to compare subgroups within each primary category.
Inspect spread and potential outliers before choosing a statistical summary.
boxplot visualizes quantitative distributions across categories. The box represents quartiles, while whiskers show the non-outlier range and points beyond the configured rule are treated as outliers.
Follow-up: How would you show every observation instead of only the box-plot summary? Add a point-based categorical plot such as stripplot alongside the boxplot.
Use x and y to assign variables, and hue to create an additional categorical subdivision. With a long-form DataFrame, these variable names select columns; with wide-form data, numeric columns can be plotted directly.
Follow-up: How can category order be made deterministic? Pass an explicit order, and use hue_order for the hue levels.
whis affect the plotted result?The default whis=1.5 makes the whisker rule a function of the interquartile range. You can instead pass another value or a pair of percentile bounds to change which observations are treated as outliers.
Follow-up: How can the display preserve actual numeric spacing for a categorical-looking axis? Enable native_scale=True, which was added for this behavior in version 0.13.0.
boxenplotstripplotswarmplotviolinplotcatplot