How to Create a Violin Plot in Seaborn



A violin plot is similar to a box plot, but also shows the distribution of the continous varaibles on the side. In a previous lesson we learned how to create a box plot to show summary statistics. In this article, we will learn how to create a Violin Plot in the Seaborn library.

Creating a Single Violin Plot

To create a basic violin plot, we use the violinplot method and pass an array of data to the x named parameter. In this example (similar to our box plot) we will create a violin plot from an array of bill totals.

import seaborn

tips = seaborn.load_dataset("tips") 
seaborn.violinplot(x = tips["total_bill"])

Creating Multiple Violin Plots

Similar to a box plot, we can also view violin plots by category. This allows us to compare the summary and distribution of a variable grouped by a category. For example, we can view the tips grouped by day to compare the difference of tips given on particular days.

To do this, we specify the continous variable (tip) in the x named parameter and the category (day) in the y named paramter.

import seaborn 
tips = seaborn.load_dataset("tips") 
seaborn.violinplot(x = "tip",
				   y = "day",
				   data = tips)
GoTea - KoalaTea