Violin Plot Bandwidth, KDE, Cut and Scaling Explained

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.

Kernel density estimation in plain terms

The outline of a violin is a kernel density estimate (KDE). Here is how it is built:

  1. Take each data point.
  2. Place a small, smooth bump (the kernel) centered on it. Most tools use a Gaussian kernel, the familiar bell curve.
  3. Add all the bumps together.
  4. Scale the total so the area under the curve equals 1.

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.

What bandwidth does to the shape

  • Too small: each bump is narrow, so the curve becomes spiky. Random gaps between points look like real dips, and a sample of 30 values can look like it has five peaks. This is undersmoothing.
  • Too large: each bump is wide, so neighboring points blur together. Two real clusters can merge into one rounded hump, and skew looks milder than it is. This is oversmoothing.
  • About right: the main features (number of peaks, skew, tail length) are visible without noise that would change if you collected new data.

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.

Bandwidth rules: Scott and Silverman

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.

Scott’s rule

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.

Silverman’s rule

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.

Where these rules go wrong

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.

Adjusting bandwidth: bw_adjust and bw_method

Most tools let you scale the automatic bandwidth instead of setting it from scratch.

  • In seaborn 0.13, 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.
  • In ggplot2, 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.

Bandwidth is set per group

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.

Cut and trimming at the data range

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:

  • seaborn uses 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.
  • ggplot2 uses 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.

Scaling options: area, count and width

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):

OptionWhat it doesGood forWatch out for
areaEvery violin has the same total areaComparing shapes when group sizes do not matterA group of 10 looks as solid as a group of 10,000
countViolin size grows with the number of observationsShowing that some groups are much largerSmall groups can become too thin to read
widthEvery violin has the same maximum widthComparing shapes when one group would otherwise be very narrowHides 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")

Why the same data can look different

If you compare a violin from one tool with a violin from another and they do not match, check these settings in order:

  1. Bandwidth rule. seaborn defaults to Scott; R and ggplot2 default to nrd0. They give different widths.
  2. Bandwidth adjustment. Any bw_adjust or adjust value other than 1.
  3. Tail handling. seaborn extends tails by default (cut=2); ggplot2 trims them (trim = TRUE).
  4. Width scaling. Area, count or width.
  5. Grid resolution. The curve is evaluated at a fixed number of points (seaborn’s gridsize defaults to 100). This rarely matters, but very coarse grids can look jagged.
  6. Inner marks. Box, quartile lines or points change how busy the shape looks, even if the outline is the same.

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.

FAQ

What is bandwidth in a violin plot?

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.

What is the default bandwidth in seaborn?

Seaborn 0.13 uses Scott’s rule by default (bw_method="scott") with bw_adjust=1. The bandwidth is computed separately for each violin.

What does cut do in seaborn’s violinplot?

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.

What replaced scale in seaborn violinplot?

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.

Why does my violin plot look different in R and Python?

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.

Adrian Lee
Adrian Lee

Adrian Lee is the Data Visualization Specialist and Statistical Analyst at ViolinPlot. Based in Boston, Massachusetts, Adrian utilizes a robust background in statistics and data science to create compelling and insightful visualizations that help interpret complex data.

With extensive experience in data analysis and visualization techniques, Adrian specializes in developing violin plots and other advanced graphical representations to convey statistical information effectively. Adrian’s expertise includes statistical modeling, data interpretation, and the creation of interactive visualizations.

At ViolinPlot, Adrian is responsible for transforming raw data into visual narratives that are both informative and engaging.

By employing state-of-the-art visualization tools and statistical methods, Adrian ensures that the visual representations are accurate, clear, and valuable for decision-making processes.

Adrian’s dedication to enhancing data comprehension through visual tools has established ViolinPlot. As a leading resource for data visualization and statistical analysis.

Articles: 89

Ready to Unlock Deeper Data Insights?

Transform complex datasets into clear, interactive visualizations with Violin Plot. Start exploring data distributions in a way that empowers insight and drives smarter decisions.

LEARN MOre
100% satisfaction guaranteed
McAfee SECURE
ssl-secured
verisign-secured
BEST-PRICE-GUARANTEED-free-trust-badge
guaranteed-safe-checkout-1