Python Central HubSign in

Data Analytics with Python / Phase 6: Statistical Visualization with Seaborn

Violin Plots

Learn violin plots for comparing distributions and how they differ from box plots.

What is a violin plot?

A violin plot combines:

  • A KDE distribution shape
  • A box-plot-like summary inside

It’s useful when you want to see the shape of distributions (multi-modal, skew).

diagram Violin = box plot + KDE mermaid
A violin plot mirrors a KDE curve left and right to form a shape, with a mini box plot drawn down the middle.

Violin plot in Seaborn

Python
import seaborn as sns
import matplotlib.pyplot as plt
 
tips = sns.load_dataset("tips")
 
plt.figure(figsize=(7, 4))
sns.violinplot(data=tips, x="day", y="total_bill")
plt.title("Total bill distribution by day")
plt.tight_layout()
plt.show()

Add inner summary

Python
import seaborn as sns
import matplotlib.pyplot as plt
 
tips = sns.load_dataset("tips")
 
plt.figure(figsize=(7, 4))
sns.violinplot(data=tips, x="day", y="total_bill", inner="quartile")
plt.title("Violin plot with quartiles")
plt.tight_layout()
plt.show()

Visualize it

The width of the violin at any height shows how common values near that height are — wider means more data points cluster there:

sketch Anatomy of a violin plot p5.js
The violin's width at each height mirrors the KDE density; the inner bar is a mini box plot.

When to prefer violin vs box

  • Use box plots for quick comparisons and outlier focus.
  • Use violin plots when the distribution shape matters.

Check your understanding

quizCheck yourself
  1. What does a violin plot combine?

    Show answer

    A — A KDE distribution shape and a box-plot-like summary — The violin outline is a KDE and the inside shows a box-like summary.

  2. Which data feature can a violin plot reveal that a box plot hides?

    Show answer

    B — Two separate peaks (bimodal data) — A box plot shows five numbers only, so bimodal data can look like one wide box.

  3. What does inner="quartile" do in sns.violinplot?

    Show answer

    B — Draws the quartile lines inside each violin — inner controls the summary drawn inside, and quartile shows Q1, median and Q3.

  4. When should you prefer a box plot over a violin plot?

    Show answer

    C — For quick comparisons and an outlier focus — Box plots suit quick comparisons and outliers. Violins suit cases where distribution shape matters.

Next

Continue to Bar Plots in Seaborn to compare an aggregated value (like a mean) across categories, with confidence intervals.

🧪 Try It Yourself

Exercise 1 – A Basic Violin Plot

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 – Show Quartiles Inside

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 – Box vs Violin

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Was this page helpful?

Let us know how we did