17

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)?

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
n1000
  • 5,058
  • 10
  • 37
  • 65
  • Well, that's probably not soo easy. This is an artefact of the KDE, which does not know that there's a hard boundary at 0. If you are interested in the problem, see also: http://stats.stackexchange.com/questions/65866/good-methods-for-density-plots-of-non-negative-variables-in-r?lq=1 – cel Jan 23 '15 at 17:48
  • @cel Thank you. that is what I was thinking. But couldn't it be fitted tighter? – n1000 Jan 23 '15 at 17:51
  • 1
    There are algorithms for that. See the impressive result in this answer: http://stats.stackexchange.com/a/71291. However I haven't seen it in python yet. – cel Jan 23 '15 at 17:53

1 Answers1

25

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.

mwaskom
  • 46,693
  • 16
  • 125
  • 127
  • 1
    I don't like the solution using `cut`. It hides the fact that `KDE` cannot fit such densities properly. The density close to the boundary 0 is misleading, since you will get such a density estimation, even if the corresponding histogram has its maximum at 0. – cel Jan 24 '15 at 09:00
  • See the feature request at https://github.com/mwaskom/seaborn/issues/525 (waiting on upstream changes in statsmodels). – naught101 Sep 14 '17 at 05:25
  • +1 Great solution. This is a quick and clean solution. It is evident, that extreme cuts will indicate some extreme differences between kde and the underlying density. – MachineLearner Aug 08 '21 at 14:03
  • I like the solution of 'Cut', it gives an idea how close your distribution is to the bounds. – Jeff Ellen Mar 01 '22 at 00:59