Keep sample size, quartiles and bandwidth visible when a smoothed shape represents observations.
Box and violin charts: compare groups without inventing density
Begin with observations
A box summarizes a group's quartiles and median; a violin adds an estimated density shape. Neither replaces the sample size. A smooth violin made from seven delivery times can look as confident as one made from seven thousand. Put each group's count beside its label and show individual points for small groups. Distribution and outlier displays explain what the summary hides. First check whether groups cover the same window, unit and eligibility rule. Otherwise the apparent difference may be a cohort change.
Declare the quartile rule
Software packages can interpolate quartiles differently, and whiskers may mean the extrema or a multiple of the interquartile range. State the rule in the chart caption or export record. A point beyond a whisker is not automatically bad data; it is flagged under that rule. If the decision needs the fraction exceeding a service threshold, calculate that fraction directly rather than asking a box to imply it. An empirical CDF answers threshold questions without a smoothing choice.
Treat bandwidth as a parameter
A violin's width depends on a density estimate and its bandwidth. A wide bandwidth can merge two real modes; a narrow one can turn routine noise into several peaks. Compare more than one defensible bandwidth, retain a rug or points, and do not imply that a thin tail continues beyond a physical limit. The mirrored shape is a drawing convention, not a second population. Normalize violin widths by count only if width should carry count. Otherwise make widths equal and show counts separately. Grain and denominator still matter.
Keep groups comparable
Use the same quantitative axis, bandwidth rule and density normalization across groups. Truncating each group to its own range can make spreads seem alike. For latency data, inspect zeros, censored values and caps before estimating density. If the data include repeated readings from the same device, an independent violin discards pairing. A paired view retains it. Supply a text table with each count, median, quartiles and threshold exceedance for readers who cannot use the shape.
Worked contract
def quartile_summary(readings: list[float]) -> dict[str, float | int]:
from math import isfinite
if not readings or any(not isfinite(value) for value in readings):
raise ValueError("readings must be finite and nonempty")
ordered = sorted(readings)
def midpoint(values: list[float]) -> float:
middle = len(values) // 2
return (values[middle] if len(values) % 2 else
(values[middle - 1] + values[middle]) / 2)
middle = len(ordered) // 2
lower = ordered[:middle]
upper = ordered[middle + len(ordered) % 2:]
return {"count": len(ordered), "median": midpoint(ordered),
"q1": midpoint(lower) if lower else ordered[0],
"q3": midpoint(upper) if upper else ordered[0]}
depot_latency = quartile_summary([47, 51, 59, 68, 81, 83, 96])
assert depot_latency == {"count": 7, "median": 68, "q1": 51, "q3": 83}Performance and operating cost
Sorting N observations costs O(N log N) time and O(N) storage for the ordered copy. A chart with G groups repeats this per group; density estimation adds work set by the estimator and evaluation grid. The code declares a median-of-halves quartile convention and handles a one-item group. It does not draw a violin or claim that seven points support a smooth density. Store the exact convention with the exported summary.
Common Mistakes
- Drawing a smooth violin for a tiny group without points.
- Comparing boxes whose whiskers use different rules.
- Treating a flagged point as an invalid measurement.
- Changing density bandwidth between groups and reading widths as a real difference.
Validation drill
Compare response times for three depots with 7, 47 and 810 records. Print each count, median and quartile rule; draw points for the smallest group, then inspect whether two bandwidth choices alter the visual claim.
Read next
- Distribution plots: keep the tail, missing values and bin policy visible
- Empirical CDFs: read thresholds and tails without choosing bins
- Paired-change charts: match entities before drawing a slope
- Uncertainty on charts: show denominators and intervals beside estimates
