👀
Crash Visualization
  • Welcome
  • Preface
    • Who the book is written for
    • How the book is organized
  • 1. Introduction of Data Visualization
    • 1.1 What is data visualization?
    • 1.2 Why does visualization matter?
  • 2. Tricks in Visualization
    • 2.1 Choose Appropriate Chart
    • 2.2 Features of Charts
      • 2.2.1 Table
      • 2.2.2 Column Chart
      • 2.2.3 Line Chart
      • 2.2.4 Pie Chart
      • 2.2.5 Scatter Chart
      • 2.2.6 Map Chart
    • 2.3 Misused Graph
    • 2.4 Tips in Visualization
  • 3. Matplotlib
    • 3.1 Basic Concepts
    • 3.2 Line Chart
    • 3.3 Area Chart
    • 3.4 Column Chart
    • 3.5 Histogram Chart
    • 3.6 Scatter Chart
    • 3.7 Lollipop Chart
    • 3.8 Pie Chart
    • 3.9 Venn Chart
    • 3.10 Waffle Chart
    • 3.11 Animation
  • 4. Seaborn
    • 4.1 Trends
    • 4.2 Ranking
      • 4.2.1 Barplot
      • 4.2.2 Boxplot
    • 4.3 Composition
      • 4.3.1 Stacked Chart
    • 4.4 Correlation
      • 4.4.1 Scatter Plot
      • 4.4.2 Linear Relationship
      • 4.4.3 Heatmap
      • 4.4.4 Pairplot
    • 4.5 Distribution
      • 4.5.1 Boxplot
      • 4.5.2 Violin plot
      • 4.5.3 Histogram plot
      • 4.5.4 Density plot
      • 4.5.5 Joint plot
  • 5. Bokeh
    • 5.1 Basic Plotting
    • 5.2 Data Sources
    • 5.3 Annotations
    • 5.4 Categorical Data
    • 5.5 Presentation and Layouts
    • 5.6 Linking and Interactions
    • 5.7 Network Graph
    • 5.8 Widgets
  • 6. Plotly
    • 6.1 Fundamental Concepts
      • 6.1.1 Plotly Express
      • 6.1.2 Plotly Graph Objects
    • 6.2 Advanced Charts
      • 6.2.1 Advanced Scatter Chart
      • 6.2.2 Advanced Bar Chart
      • 6.2.3 Advanced Pie Chart
      • 6.2.4 Advanced Heatmap
      • 6.2.5 Sankey Chart
      • 6.2.6 Tables
    • 6.3 Statistical Charts
      • 6.3.1 Common Statistical Charts
      • 6.3.2 Dendrograms
      • 6.3.3 Radar Chart
      • 6.3.4 Polar Chart
      • 6.3.5 Streamline Chart
    • 6.4 Financial Charts
      • 6.4.1 Funnel Chart
      • 6.4.2 Candlestick Chart
      • 6.4.3 Waterfall Chart
  • Support
    • Donation
Powered by GitBook
On this page
  • Basic violin plot
  • Horizontal violin plot
  • Grouped violin plot
  • Grouped violin plot with split violins

Was this helpful?

  1. 4. Seaborn
  2. 4.5 Distribution

4.5.2 Violin plot

Previous4.5.1 BoxplotNext4.5.3 Histogram plot

Last updated 4 years ago

Was this helpful?

A violin plot is a method of plotting numeric data. It is similar to a box plot, with the addition of a rotated kernel density plot on each side. Violin plots are similar to box plots, except that they also show the probability density of the data at different values, usually smoothed by a kernel density estimator.

However, the violin plots can be harder to read and it not commonly used in professional settings.

Let's use the "mpg" and "tips" datasets for example.

import seaborn as sns
plt.style.use('seaborn-white')
plt.rcParams.update({'font.size': 18, 'figure.figsize':(12, 6)})

mpg = sns.load_dataset('mpg')
tips = sns.load_dataset('tips')

Basic violin plot

sns.violinplot(x='origin', y='horsepower', data=mpg, 
                scale='width', inner='quartile', palette = 'husl')

This violin plot shows the relationship of feed type to chick weight. The box plot elements show the median weight for horsebean-fed chicks is lower than for other feed types. The shape of the distribution (extremely skinny on each end and wide in the middle) indicates the weights of sunflower-fed chicks are highly concentrated around the median.

Horizontal violin plot

Like horizontal bar charts, horizontal violin plots are ideal for dealing with many categories. Swapping axes gives the category labels more room to breathe.

# very straightforward, just change the 'x' and 'y'
sns.violinplot(y='origin', x='mpg', data=mpg, 
                scale='width', inner='quartile', palette = 'husl')

Grouped violin plot

Violin plots can also illustrate a second-order categorical variable. You can create groups within each category.

# add a 'hue' parameter to differentiate
sns.violinplot(x='cylinders', y='horsepower', hue = 'origin', data=mpg, 
                scale='width', inner='quartile',palette = 'husl')

Grouped violin plot with split violins

tips = sns.load_dataset('tips')

Example 1

sns.violinplot(x='size', y='total_bill', hue = 'time', data=tips,
        split = True, palette={'Dinner': 'dodgerblue', 'Lunch': 'salmon'})
        
# Put the legend out of the figure
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)

Example 2

sns.violinplot(x='day', y='tip', hue = 'sex', data=tips,split = True, 
                palette={'Male': 'dodgerblue', 'Female': 'salmon'})