Phone:+16179821253
Address: 315 W 3rd St, Boston, Massachusetts, 02127, USA
Email: sloanecarter773@gmail.com
Phone:+16179821253
Address: 315 W 3rd St, Boston, Massachusetts, 02127, USA
Email: sloanecarter773@gmail.com
Why the same data can produce different violin shapes, explained through bandwidth, trimming and width scaling.
The bandwidth of a violin plot controls how much the density curve is smoothed. A small bandwidth follows the data closely and shows every bump; a large bandwidth blurs detail into a smooth, rounded shape. Bandwidth, together with how the tails are trimmed and how widths are scaled, is why the same numbers can produce violins that look quite different in two tools.
This article explains kernel density estimation in plain terms, the common bandwidth rules (Scott and Silverman), the bw_adjust setting, trimming with cut, and the three width scaling options.
The outline of a violin is a kernel density estimate (KDE). Here is how it is built:
Where data points cluster, their bumps overlap and stack up into a peak. Where points are sparse, the curve stays low. A violin plot takes that curve, turns it on its side, and mirrors it.
The bandwidth is the width of each bump. For a Gaussian kernel it is the standard deviation of the bell curve placed on each point. That one number has a large effect on the final shape. You can see the effect for yourself by pasting a column of numbers into a free violin plot generator and comparing the result with the same data in another tool.
For an introduction to the chart itself, see the violin plot guide. If you want to understand what the finished shape tells you, read how to read a violin plot.
There is no single correct bandwidth. Software uses a rule of thumb to pick a reasonable starting value, and you can adjust it from there.
Both common rules are “normal reference” rules. They pick the bandwidth that would work well if the data were roughly normal, based on two things: how spread out the data are and how many points there are. Both shrink the bandwidth as the sample grows, in proportion to n to the power of minus one fifth. More data means less smoothing is needed.
In seaborn, which uses SciPy’s density code, Scott’s rule sets the bandwidth to the group’s standard deviation multiplied by n-1/5. This is seaborn’s default.
In SciPy and seaborn, the Silverman option uses the factor (3n/4)-1/5, which works out to about 6% larger than Scott’s for the same data. So in seaborn, choosing Silverman gives slightly smoother violins.
R uses a different version, also credited to Silverman, as its default for density() and for ggplot2’s geom_violin(): bandwidth = 0.9 × min(standard deviation, IQR / 1.34) × n-1/5. Taking the smaller of the standard deviation and the scaled IQR makes it less likely to oversmooth when the data are skewed or have outliers. This is one reason the same data can look a little different in R and Python.
Rules based on the standard deviation assume one peak. When data are bimodal, the standard deviation is large (the two clusters are far apart), so the rule picks a wide bandwidth and can smooth the two peaks into one. If you suspect multiple peaks, try a smaller bandwidth and see whether a dip appears between them, then confirm by looking at the raw points.
Most tools let you scale the automatic bandwidth instead of setting it from scratch.
bw_adjust multiplies the bandwidth chosen by the rule. bw_adjust=0.5 halves it (more detail), bw_adjust=2 doubles it (smoother). bw_method picks the rule ("scott", "silverman") or takes a number used as the scale factor directly.geom_violin() has bw to choose the rule (default "nrd0") and adjust to multiply it, which works like bw_adjust.Older seaborn code used a bw parameter for this. In 0.13 it was deprecated in favor of bw_method and bw_adjust.
Here is a quick way to compare three bandwidths side by side:
import seaborn as sns
import matplotlib.pyplot as plt
# df has a "group" column and a numeric "value" column
fig, axes = plt.subplots(1, 3, figsize=(12, 4), sharey=True)
for ax, adj in zip(axes, [0.5, 1, 2]):
sns.violinplot(data=df, x="group", y="value",
bw_adjust=adj, cut=0, ax=ax)
ax.set_title(f"bw_adjust={adj}")
plt.savefig("bandwidth.png", dpi=150)
If a feature, such as a second peak, appears at bw_adjust=0.5 and 1 but disappears at 2, it is probably real but modest. If it only shows at 0.5, treat it with caution.
In both seaborn and ggplot2, the rule is applied to each violin’s own data. A group with a wide spread gets a wider bandwidth than a tight group. That is usually what you want, but it means two violins on the same chart are not smoothed by the same amount. When groups differ a lot in spread or size, some shape differences come partly from smoothing.
A Gaussian kernel never quite reaches zero, so the density curve extends past the smallest and largest data points. Tools handle this in different ways:
cut, measured in bandwidths. The default is cut=2, so each violin extends two bandwidths beyond its extreme values. cut=0 stops the violin at the data range.trim. The default, trim = TRUE, stops each violin at the data range. trim = FALSE lets the tails run past it.Extended tails look smooth, but they can show values that do not exist. A violin for response times that dips below zero seconds is a common example. For variables with hard limits (non-negative amounts, percentages, scores on a fixed scale), trimming to the data range is usually the safer choice.
Trimming does not fix everything. A KDE near a hard boundary still spreads some of its weight past that boundary, so the curve near the edge can look thinner than the data justify. Some tools offer boundary corrections for this (recent ggplot2 releases added a bounds argument to their density stats; check the documentation for your version). If the edge matters, check the raw points.
After each density is computed, the tool has to decide how wide to draw each violin relative to the others. There are three common options, with the same names in seaborn (density_norm) and ggplot2 (scale):
| Option | What it does | Good for | Watch out for |
|---|---|---|---|
| area | Every violin has the same total area | Comparing shapes when group sizes do not matter | A group of 10 looks as solid as a group of 10,000 |
| count | Violin size grows with the number of observations | Showing that some groups are much larger | Small groups can become too thin to read |
| width | Every violin has the same maximum width | Comparing shapes when one group would otherwise be very narrow | Hides both size and peak density differences |
Area is the default in both seaborn and ggplot2. The count option differs slightly between them: ggplot2 scales each violin’s area by its number of observations, while seaborn scales each violin’s maximum width by it. In seaborn 0.13 this parameter is called density_norm; older code used scale, which still runs but is deprecated. Similarly, common_norm replaced scale_hue, and controls whether normalization is shared across all violins or done separately within each hue level.
sns.violinplot(data=df, x="group", y="value", bw_method="silverman")
sns.violinplot(data=df, x="group", y="value", density_norm="count")
If you compare a violin from one tool with a violin from another and they do not match, check these settings in order:
bw_adjust or adjust value other than 1.cut=2); ggplot2 trims them (trim = TRUE).gridsize defaults to 100). This rarely matters, but very coarse grids can look jagged.None of these settings is wrong. They are choices, and a good chart states them or uses defaults that suit the data. If your groups are small, the outline is especially sensitive to these settings; adding the raw observations helps, as covered in violin plots with data points. And if you are deciding whether a smoothed chart is right at all, see violin plot vs box plot.
To experiment with styles on your own numbers without writing code, try our online violin plot maker.
It is the width of the smoothing kernel used to build the density curve. Smaller bandwidth shows more detail and noise; larger bandwidth gives a smoother shape and can hide real features.
Seaborn 0.13 uses Scott’s rule by default (bw_method="scott") with bw_adjust=1. The bandwidth is computed separately for each violin.
It sets how far the density extends past the most extreme data points, in units of bandwidth. The default is 2. Use cut=0 to keep the violin inside the data range.
In seaborn 0.13, scale was renamed density_norm, scale_hue became common_norm, and bw was split into bw_method and bw_adjust. The old names are deprecated.
The defaults differ. R uses the nrd0 bandwidth rule and trims tails to the data range; seaborn uses Scott’s rule and extends tails by two bandwidths. Match the bandwidth, trimming and scaling settings to get similar shapes.
Transform complex datasets into clear, interactive visualizations with Violin Plot. Start exploring data distributions in a way that empowers insight and drives smarter decisions.



