Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to better fit seaborn violinplots?

The following code gives me a very nice violinplot (and boxplot within).

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

foo = np.random.rand(100)
sns.violinplot(foo)
plt.boxplot(foo)
plt.show()

output

So far so good. However, when I look at foo, the variable does not contain any negative values. The seaborn plot seems misleading here. The normal matplotlib boxplot gives something closer to what I would expect.

How can I make violinplots with a better fit (not showing false negative values)?

like image 672
n1000 Avatar asked Jan 23 '15 17:01

n1000


People also ask

Are violin plots better or worse than box plots?

A violin plot is more informative than a plain box plot. While a box plot only shows summary statistics such as mean/median and interquartile ranges, the violin plot shows the full distribution of the data. The difference is particularly useful when the data distribution is multimodal (more than one peak).

How do you remove outliers in violin plot Seaborn?

You can do it by excluding the outlier data while passing it through the plot function. e.g. wherein, df is your dataframe. Column is the name of the column you want to plot and x is the outlier value that you want to exclude.

For which scenario violin plot is more appropriate?

Violin plots are used when you want to observe the distribution of numeric data, and are especially useful when you want to make a comparison of distributions between multiple groups. The peaks, valleys, and tails of each group's density curve can be compared to see where groups are similar or different.


1 Answers

As the comments note, this is a consequence (I'm not sure I'd call it an "artifact") of the assumptions underlying gaussian KDE. As has been mentioned, this is somewhat unavoidable, and if your data don't meet those assumptions, you might be better off just using a boxplot, which shows only points that exist in the actual data.

However, in your response you ask about whether it could be fit "tighter", which could mean a few things.

One answer might be to change the bandwidth of the smoothing kernel. You do that with the bw argument, which is actually a scale factor; the bandwidth that will be used is bw * data.std():

data = np.random.rand(100)
sns.violinplot(y=data, bw=.1)

enter image description here

Another answer might be to truncate the violin at the extremes of the datapoints. The KDE will still be fit with densities that extend past the bounds of your data, but the tails will not be shown. You do that with the cut parameter, which specifies how many units of bandwidth past the extreme values the density should be drawn. To truncate, set it to 0:

sns.violinplot(y=data, cut=0)

enter image description here

By the way, the API for violinplot is going to change in 0.6, and I'm using the development version here, but both the bw and cut arguments exist in the current released version and behave more or less the same way.

like image 152
mwaskom Avatar answered Nov 04 '22 03:11

mwaskom