Box & Violin Plots
Purpose
Box plots and violin plots compare distributions across groups. They highlight:
- Median and quartiles (box plot).
- Full distribution shape (violin plot).
- Outliers and range.
Example in the Notebook
The notebook displays total bill by day using:
- A box plot (left panel) — shows median, IQR, whiskers, and outliers.
- A violin plot (right panel) — shows the full distribution shape.
- An overlaid swarm plot (right panel) — individual data points.
Key Code Snippet
sns.boxplot(data=tips, x='day', y='total_bill', hue='day', dodge=False, ax=axes[0], palette='pastel')
sns.violinplot(data=tips, x='day', y='total_bill', hue='day', split=False, inner=None, ax=axes[1], palette='muted')
sns.swarmplot(data=tips, x='day', y='total_bill', color='k', size=3, ax=axes[1])
Customization Tips
- Boxplot parts: Show quartiles, mean, or whiskers with
showmeans=True,showfliers=True. - Violin plot shape: Use
split=Trueto compare two hue groups side-by-side (for 2-level categorical). - Inner representation: Set
inner='box',inner='quartile', orinner=None(violin only). - Point overlay: Add
sns.swarmplot()orsns.stripplot()for individual observations. - Orient: Use
orient='h'for horizontal layout.
When to Use
- Box plots: quick comparison of medians and spreads across many groups.
- Violin plots: when distribution shape matters more than individual points.
- Swarm overlay: when sample size is small (< 50 per group) and you want to see all points.
See Also
- Seaborn: boxplot(), violinplot(), swarmplot()