seaborn.
violinplot
(x=None, y=None, hue=None, data=None, order=None, hue_order=None, bw='scott', cut=2, scale='area', scale_hue=True, gridsize=100, width=0.8, inner='box', split=False, dodge=True, orient=None, linewidth=None, color=None, palette=None, saturation=0.75, ax=None, **kwargs)¶Draw a combination of boxplot and kernel density estimate.
A violin plot plays a similar role as a box and whisker plot. It shows the distribution of quantitative data across several levels of one (or more) categorical variables such that those distributions can be compared. Unlike a box plot, in which all of the plot components correspond to actual datapoints, the violin plot features a kernel density estimation of the underlying distribution.
This can be an effective and attractive way to show multiple distributions of data at once, but keep in mind that the estimation procedure is influenced by the sample size, and violins for relatively small samples might look misleadingly smooth.
Input data can be passed in a variety of formats, including:
x
, y
, and/or hue
parameters.x
, y
, and hue
variables will determine how the data are plotted.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.
Parameters: | x, y, hue : names of variables in
data : DataFrame, array, or list of arrays, optional
order, hue_order : lists of strings, optional
bw : {‘scott’, ‘silverman’, float}, optional
cut : float, optional
scale : {“area”, “count”, “width”}, optional
scale_hue : bool, optional
gridsize : int, optional
width : float, optional
inner : {“box”, “quartile”, “point”, “stick”, None}, optional
split : bool, optional
dodge : bool, optional
orient : “v” | “h”, optional
linewidth : float, optional
color : matplotlib color, optional
palette : palette name, list, or dict, optional
saturation : float, optional
ax : matplotlib Axes, optional
|
---|---|
Returns: | ax : matplotlib Axes
|
See also
boxplot
stripplot
swarmplot
Examples
Draw a single horizontal violinplot:
>>> import seaborn as sns
>>> sns.set(style="whitegrid")
>>> tips = sns.load_dataset("tips")
>>> ax = sns.violinplot(x=tips["total_bill"])
Draw a vertical violinplot grouped by a categorical variable:
>>> ax = sns.violinplot(x="day", y="total_bill", data=tips)
Draw a violinplot with nested grouping by two categorical variables:
>>> ax = sns.violinplot(x="day", y="total_bill", hue="smoker",
... data=tips, palette="muted")
Draw split violins to compare the across the hue variable:
>>> ax = sns.violinplot(x="day", y="total_bill", hue="smoker",
... data=tips, palette="muted", split=True)
Control violin order by passing an explicit order:
>>> ax = sns.violinplot(x="time", y="tip", data=tips,
... order=["Dinner", "Lunch"])
Scale the violin width by the number of observations in each bin:
>>> ax = sns.violinplot(x="day", y="total_bill", hue="sex",
... data=tips, palette="Set2", split=True,
... scale="count")
Draw the quartiles as horizontal lines instead of a mini-box:
>>> ax = sns.violinplot(x="day", y="total_bill", hue="sex",
... data=tips, palette="Set2", split=True,
... scale="count", inner="quartile")
Show each observation with a stick inside the violin:
>>> ax = sns.violinplot(x="day", y="total_bill", hue="sex",
... data=tips, palette="Set2", split=True,
... scale="count", inner="stick")
Scale the density relative to the counts across all bins:
>>> ax = sns.violinplot(x="day", y="total_bill", hue="sex",
... data=tips, palette="Set2", split=True,
... scale="count", inner="stick", scale_hue=False)
Use a narrow bandwidth to reduce the amount of smoothing:
>>> ax = sns.violinplot(x="day", y="total_bill", hue="sex",
... data=tips, palette="Set2", split=True,
... scale="count", inner="stick",
... scale_hue=False, bw=.2)
Draw horizontal violins:
>>> planets = sns.load_dataset("planets")
>>> ax = sns.violinplot(x="orbital_period", y="method",
... data=planets[planets.orbital_period < 1000],
... scale="width", palette="Set3")
Don’t let density extend past extreme values in the data:
>>> ax = sns.violinplot(x="orbital_period", y="method",
... data=planets[planets.orbital_period < 1000],
... cut=0, scale="width", palette="Set3")
Use hue
without changing violin position or width:
>>> tips["weekend"] = tips["day"].isin(["Sat", "Sun"])
>>> ax = sns.violinplot(x="day", y="total_bill", hue="weekend",
... data=tips, dodge=False)
Use catplot()
to combine a violinplot()
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="violin", split=True,
... height=4, aspect=.7);