Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Drawing Violin Plots | Section
Statistical Visualization with Seaborn

bookDrawing Violin Plots

A violinplot is a hybrid of a box plot and a kernel density plot (KDE).

While a boxplot only shows summary statistics (median, quartiles), a violinplot reveals the full distribution of the data. The "width" of the violin at any given point represents the density (how many data points are there).

Key Parameters

  • split=True: if you have a hue variable with exactly two categories (e.g., "Male"/"Female"), this parameter draws one category on the left half of the violin and the other on the right. This makes comparing them incredibly easy;
  • inner: controls what is drawn inside the violin;
  • 'box' (default): draws a mini boxplot;
  • 'point': draws individual data points;
  • 'quartile': draws lines for the 25th, 50th, and 75th percentiles;
  • bw (bandwidth): controls the smoothness of the curve (just like in KDE). A smaller number reveals more detail (and noise); a larger number makes it smoother.

Example

Here is a violinplot showing the total_bill distribution. Notice how split=True allows us to compare "Smokers" vs "Non-Smokers" inside the same violin.

123456789101112131415161718
import seaborn as sns import matplotlib.pyplot as plt # Load dataset df = sns.load_dataset('tips') # Create a split violinplot sns.violinplot( data=df, x='day', y='total_bill', hue='smoker', split=True, # Compare sides directly inner='quartile', # Show quartile lines palette='muted' ) plt.show()
copy
Task

Swipe to start coding

Create a detailed visualization of the tips data.

  1. Import the necessary libraries and read the tips.csv dataset.
  2. Create a violinplot and assign the result to a variable named g (this captures the plot's Axes object):
    • Map 'day' to x and 'total_bill' to y.
    • Group by 'sex' using hue.
    • Use the 'rocket' palette.
    • Split the violins to compare genders side-by-side (split=True).
    • Show individual data points inside by setting inner='point'.
    • Set the smoothing bandwidth bw to 0.2.
  3. Set the title of the plot to 'Tips violinplot' using the g variable (e.g., g.set_title(...)).
  4. Display the plot.

Solution

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 12
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

close

bookDrawing Violin Plots

Swipe to show menu

A violinplot is a hybrid of a box plot and a kernel density plot (KDE).

While a boxplot only shows summary statistics (median, quartiles), a violinplot reveals the full distribution of the data. The "width" of the violin at any given point represents the density (how many data points are there).

Key Parameters

  • split=True: if you have a hue variable with exactly two categories (e.g., "Male"/"Female"), this parameter draws one category on the left half of the violin and the other on the right. This makes comparing them incredibly easy;
  • inner: controls what is drawn inside the violin;
  • 'box' (default): draws a mini boxplot;
  • 'point': draws individual data points;
  • 'quartile': draws lines for the 25th, 50th, and 75th percentiles;
  • bw (bandwidth): controls the smoothness of the curve (just like in KDE). A smaller number reveals more detail (and noise); a larger number makes it smoother.

Example

Here is a violinplot showing the total_bill distribution. Notice how split=True allows us to compare "Smokers" vs "Non-Smokers" inside the same violin.

123456789101112131415161718
import seaborn as sns import matplotlib.pyplot as plt # Load dataset df = sns.load_dataset('tips') # Create a split violinplot sns.violinplot( data=df, x='day', y='total_bill', hue='smoker', split=True, # Compare sides directly inner='quartile', # Show quartile lines palette='muted' ) plt.show()
copy
Task

Swipe to start coding

Create a detailed visualization of the tips data.

  1. Import the necessary libraries and read the tips.csv dataset.
  2. Create a violinplot and assign the result to a variable named g (this captures the plot's Axes object):
    • Map 'day' to x and 'total_bill' to y.
    • Group by 'sex' using hue.
    • Use the 'rocket' palette.
    • Split the violins to compare genders side-by-side (split=True).
    • Show individual data points inside by setting inner='point'.
    • Set the smoothing bandwidth bw to 0.2.
  3. Set the title of the plot to 'Tips violinplot' using the g variable (e.g., g.set_title(...)).
  4. Display the plot.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 12
single

single

some-alt