Professional Documents
Culture Documents
Violin Plots Explained
Violin Plots Explained
Violin plots explained. Learn how to use violin plots and what… | by Eryk Lewinson | Towards Data Science
This is your last free member-only story this month. Upgrade for unlimited access.
Save
Source: Unsplash
the lower/upper adjacent values (the black lines stretched from the bar) — defined as first quartile — 1.5 IQR and third
quartile + 1.5 IQR respectively. These values can be used in a simple outlier detection technique (Tukey’s fences) —
observations lying outside of these “fences” can be considered
746 outliers.
2
https://towardsdatascience.com/violin-plots-explained-fb1d115e023d 1/8
25/09/2022, 22:45 Violin plots explained. Learn how to use violin plots and what… | by Eryk Lewinson | Towards Data Science
Source
The unquestionable advantage of the violin plot over the box plot is that aside from showing the abovementioned statistics it also
shows the entire distribution of the data. This is of interest, especially when dealing with multimodal data, i.e., a distribution with
more than one peak.
Implementation in Python
In this article we use the following libraries:
seaborn 0.9.0
numpy 1.17.2
pandas 0.25.1
matplotlib 3.1.1
We start by defining the number of random observations we will draw from certain distributions, as well as setting the seed for
reproducibility of the results.
N = 10 ** 4
np.random.seed(42)
a boxplot
a violin plot
We will use this function for inspecting the randomly created samples.
sns.distplot(x, ax=ax[0])
ax[0].set_title('Histogram + KDE')
sns.boxplot(x, ax=ax[1])
ax[1].set_title('Boxplot')
sns.violinplot(x, ax=ax[2])
ax[2].set_title('Violin plot')
fig.suptitle(title, fontsize=16)
plt.show()
https://towardsdatascience.com/violin-plots-explained-fb1d115e023d 2/8
25/09/2022, 22:45 Violin plots explained. Learn how to use violin plots and what… | by Eryk Lewinson | Towards Data Science
We start with the most basic distribution — Standard Normal. We draw 10000 numbers at random and plot the results.
Get unlimited access Open in app
sample_gaussian = np.random.normal(size=N)
we can see the previously mentioned metrics (median, IQR, Tukey’s fences) in both the box plot as well as the violin plot
the kernel density plot used for creating the violin plot is the same as the one added on top of the histogram. Wider sections of
the violin plot represent a higher probability of observations taking a given value, the thinner sections correspond to a lower
probability.
I believe that showing these three plots together provides good intuition to what a violin plot actually is and what kind of
information it contains.
Log-normal distribution
In the second example, we consider the log-normal distribution, which is definitely more skewed than the Normal distribution.
sample_lognormal = np.random.lognormal(size=N)
https://towardsdatascience.com/violin-plots-explained-fb1d115e023d 3/8
25/09/2022, 22:45 Violin plots explained. Learn how to use violin plots and what… | by Eryk Lewinson | Towards Data Science
bimodal_dist.py
hosted with ❤ by GitHub view raw
Without looking at a histogram/density plot, it would be impossible to spot the two peaks in our data.
https://towardsdatascience.com/violin-plots-explained-fb1d115e023d 4/8
25/09/2022, 22:45 Violin plots explained. Learn how to use violin plots and what… | by Eryk Lewinson | Towards Data Science
Advanced usage
Violin plots are often used to compare the distribution of a given variable across some categories. We present a few of the
possibilities below. To do so, we load the tips dataset from seaborn .
tips = sns.load_dataset("tips")
In the first example, we look at the distribution of the tips per gender. Additionally, we change the structure of the violin plot to
display the quartiles only. Some other possibilities include point for showing all the observations or box for drawing a small box
plot inside the violin plot.
https://towardsdatascience.com/violin-plots-explained-fb1d115e023d 5/8
25/09/2022, 22:45 Violin plots explained. Learn how to use violin plots and what… | by Eryk Lewinson | Towards Data Science
We see that the overall shape and distribution of the tips are similar for both genders (quartiles very close to each other), but there
are more outliers in the case of males.
In the second example, we investigate the distribution of the total bill amount per day. Additionally, we split by gender.
Immediately we see that the largest difference in the shape of the distribution between genders happens on Fridays.
https://towardsdatascience.com/violin-plots-explained-fb1d115e023d 6/8
25/09/2022, 22:45 Violin plots explained. Learn how to use violin plots and what… | by Eryk Lewinson | Towards Data Science
In the last example, we investigate the same thing as in the previous case, however, we set split=True . By doing so, instead of 8
Get unlimited access Open in app
violins, we end up with four — each side of the violin corresponds to a different gender.
Conclusions
In this article, I showed what are the violin plots, how to interpret them and what are their advantages over the box plots. One last
remark worth making is that the box plots do not adapt as long as the quartiles stay the same. We can modify the data in a way
that the quartiles do not change, but the shape of the distribution differs dramatically. The following GIF illustrates the point.
Source
As always, any constructive feedback is welcome. You can reach out to me on Twitter or in the comments. You can find the code
used for this article on my GitHub.
Liked the article? Become a Medium member to continue learning by reading without limits. If you use this link to become a
member, you will support me at no extra cost to you. Thanks in advance and see you around!
https://towardsdatascience.com/violin-plots-explained-fb1d115e023d 7/8
25/09/2022, 22:45 Violin plots explained. Learn how to use violin plots and what… | by Eryk Lewinson | Towards Data Science
Every Thursday, the Variable delivers the very best of Towards Data Science: from hands-on tutorials and cutting-edge research to original features you don't want to miss. Take a
look.
https://towardsdatascience.com/violin-plots-explained-fb1d115e023d 8/8