- 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)#
Draw a box plot to show distributions with respect to categories.
A box plot (or box-and-whisker plot) shows the distribution of quantitativedata in a way that facilitates comparisons between variables or acrosslevels of a categorical variable. The box shows the quartiles of thedataset while the whiskers extend to show the rest of the distribution,except for points that are determined to be “outliers” using a methodthat is a function of the inter-quartile range.
See the tutorial for more information.
Note
By default, this function treats one of the variables as categoricaland draws data at ordinal positions (0, 1, … n) on the relevant axis.As of version 0.13.0, this can be disabled by setting
native_scale=True
.- Parameters:
- dataDataFrame, Series, dict, array, or list of arrays
Dataset for plotting. If
x
andy
are absent, this isinterpreted as wide-form. Otherwise it is expected to be long-form.- x, y, huenames of variables in
data
or vector data Inputs for plotting long-form data. See examples for interpretation.
- order, hue_orderlists of strings
Order to plot the categorical levels in; otherwise the levels areinferred from the data objects.
- orient“v” | “h” | “x” | “y”
Orientation of the plot (vertical or horizontal). This is usuallyinferred based on the type of the input variables, but it can be usedto resolve ambiguity when both
x
andy
are numeric or whenplotting wide-form data.Changed in version v0.13.0: Added ‘x’/’y’ as options, equivalent to ‘v’/’h’.
- colormatplotlib color
Single color for the elements in the plot.
- palettepalette name, list, or dict
Colors to use for the different levels of the
hue
variable. Shouldbe something that can be interpreted by color_palette(), or adictionary mapping hue levels to matplotlib colors.- saturationfloat
Proportion of the original saturation to draw fill colors in. Largepatches often look better with desaturated colors, but set this to
1
if you want the colors to perfectly match the input values.- fillbool
If True, use a solid patch. Otherwise, draw as line art.
New in version v0.13.0.
- dodge“auto” or bool
When hue mapping is used, whether elements should be narrowed and shifted alongthe orient axis to eliminate overlap. If
"auto"
, set toTrue
when theorient variable is crossed with the categorical variable orFalse
otherwise.Changed in version 0.13.0: Added
"auto"
mode as a new default.- widthfloat
Width allotted to each element on the orient axis. When
native_scale=True
,it is relative to the minimum distance between two values in the native scale.- gapfloat
Shrink on the orient axis by this factor to add a gap between dodged elements.
New in version 0.13.0.
- whisfloat or pair of floats
Paramater that controls whisker length. If scalar, whiskers are drawnto the farthest datapoint within whis * IQR from the nearest hinge.If a tuple, it is interpreted as percentiles that whiskers represent.
- linecolorcolor
Color to use for line elements, when
fill
is True.New in version v0.13.0.
- linewidthfloat
Width of the lines that frame the plot elements.
- fliersizefloat
Size of the markers used to indicate outlier observations.
- hue_normtuple or
matplotlib.colors.Normalize
object Normalization in data units for colormap applied to the
hue
variable when it is numeric. Not relevant ifhue
is categorical.New in version v0.12.0.
- log_scalebool or number, or pair of bools or numbers
Set axis scale(s) to log. A single value sets the data axis for any numericaxes in the plot. A pair of values sets each axis independently.Numeric values are interpreted as the desired base (default 10).When
None
orFalse
, seaborn defers to the existing Axes scale.New in version v0.13.0.
- native_scalebool
When True, numeric or datetime values on the categorical axis will maintaintheir original scaling rather than being converted to fixed indices.
New in version v0.13.0.
- formattercallable
Function for converting categorical data into strings. Affects both groupingand tick labels.
New in version v0.13.0.
- legend“auto”, “brief”, “full”, or False
How to draw the legend. If “brief”, numeric
hue
andsize
variables will be represented with a sample of evenly spaced values.If “full”, every group will get an entry in the legend. If “auto”,choose between brief or full representation based on number of levels.IfFalse
, no legend data is added and no legend is drawn.New in version v0.13.0.
- axmatplotlib Axes
Axes object to draw the plot onto, otherwise uses the current Axes.
- kwargskey, value mappings
Other keyword arguments are passed through to
matplotlib.axes.Axes.boxplot()
.
- Returns:
- axmatplotlib Axes
Returns the Axes object with the plot drawn onto it.
See also
- violinplot
A combination of boxplot and kernel density estimation.
- stripplot
A scatterplot where one variable is categorical. Can be used in conjunction with other plots to show each observation.
- swarmplot
A categorical scatterplot where the points do not overlap. Can be used with other plots to show each observation.
- catplot
Combine a categorical plot with a FacetGrid.
Examples
Draw a single horizontal boxplot, assigning the data directly to the coordinate variable:
sns.boxplot(x=titanic["age"])
Group by a categorical variable, referencing columns in a dataframe:
sns.boxplot(data=titanic, x="age", y="class")
Draw a vertical boxplot with nested grouping by two variables:
sns.boxplot(data=titanic, x="class", y="age", hue="alive")
Draw the boxes as line art and add a small gap between them:
sns.boxplot(data=titanic, x="class", y="age", hue="alive", fill=False, gap=.1)
Cover the full range of the data with the whiskers:
sns.boxplot(data=titanic, x="age", y="deck", whis=(0, 100))
Draw narrower boxes:
sns.boxplot(data=titanic, x="age", y="deck", width=.5)
Modify the color and width of all the line artists:
sns.boxplot(data=titanic, x="age", y="deck", color=".8", linecolor="#137", linewidth=.75)
Group by a numeric variable and preserve its native scaling:
ax = sns.boxplot(x=titanic["age"].round(-1), y=titanic["fare"], native_scale=True)ax.axvline(25, color=".3", dashes=(2, 2))
Customize the plot using parameters of the underlying matplotlib function:
sns.boxplot( data=titanic, x="age", y="class", notch=True, showcaps=False, flierprops={"marker": "x"}, boxprops={"facecolor": (.3, .5, .7, .5)}, medianprops={"color": "r", "linewidth": 2},)
FAQs
What is the syntax for Seaborn boxplot? ›
While the points are plotted in two dimensions, another dimension can be added to the plot by coloring the points according to a third variable. Syntax: seaborn. boxplot(x, y, hue, data);
What is the alternative to the boxplot in Seaborn? ›A violin plot plays a similar role as a box-and-whisker plot. It shows the distribution of data points after grouping by one (or more) variables. Unlike a box plot, each violin is drawn using a kernel density estimate of the underlying distribution.
How do you change the size of a Seaborn boxplot? ›Changing the Figure Size of a Seaborn Boxplot Method 2
An alternative way of changing the size of a Seaborn plot is to call upon sns. set(rc={“figure. figsize”:(10, 10)}) .
Adding labels and a title to a box plot
In seaborn , we use the x and y parameters of the sns. boxplot() function to label the x and y-axis and the . set() to add a title to the plot.
Creating Box Plot
boxplot() method can be a Numpy array or Python list or Tuple of arrays. Let us create the box plot by using numpy. random. normal() to create some random data, it takes mean, standard deviation, and the desired number of values as arguments.
A box plot is constructed from five values: the minimum value, the first quartile, the median, the third quartile, and the maximum value. We use these values to compare how close other data values are to them.
How to set Seaborn plot size? ›Seaborn provides a convenient function called 'set_context()' that allows us to adjust the plot's overall style, including the size. The 'set_context()' function has a parameter called 'rc' that accepts a dictionary of Matplotlib parameters. We can specify the size of the plot using the 'figure. figsize' parameter.
How do I make my boxplot size bigger? ›First calculate the proportion of each level using the table() function. Using these proportions will make the box twice bigger if a level is twice more represented. Then give these proportions to the width argument when you call the boxplot() function.
How do I resize a boxplot in Python? ›Set the figure size and adjust the padding between and around the subplots. Make a Pandas dataframe, i.e., two-dimensional, size-mutable, potentially heterogeneous tabular data. Make a box and whisker plot, using boxplot() method with width tuple to adjust the box in boxplot. To display the figure, use show() method.
How do you hide outliers in Boxplot seaborn? ›Seaborn has already been imported as sns and matplotlib. pyplot has been imported as plt . As a reminder, you can omit outliers in box plots by setting the sym parameter equal to an empty string ( "" ).
How do you specify colors in seaborn Boxplot? ›
The recommended way to change the colors in Seaborn is to convert the dataframe to long form via pandas' melt() and then use hue= on the same variable as x= together with palette= . ( palette can be, among others, a dictionary or a list of colors.)
How do you remove whiskers from a seaborn box plot? ›You can remove the outliers by setting showfliers=False and remove whiskers by setting whis=0 .
What is the correct syntax for importing seaborn? ›By convention, it is imported with the shorthand sns . Behind the scenes, seaborn uses matplotlib to draw its plots. For interactive work, it's recommended to use a Jupyter/IPython interface in matplotlib mode, or else you'll have to call matplotlib.pyplot.show() when you want to see the plot.
What is the syntax of distplot plot in Python? ›Syntax: distplot(a[, bins, hist, kde, rug, fit, ...]) Example: Python3.
What is the syntax for seaborn subplot? ›You can use the following basic syntax to create subplots in the seaborn data visualization library in Python: #define dimensions of subplots (rows, columns) fig, axes = plt. subplots(2, 2) #create chart in each subplot sns. boxplot(data=df, x='team', y='points', ax=axes[0,0]) sns.
What is the syntax to create a line plot in seaborn? ›Instead, in Seaborn, lineplot() or relplot() with kind = 'line' must be preferred. Line plots give annotation to each of the points and plus helps in customizing markers, line style, and legends. Parameters: x, y: Input data variables; must be numeric.