Feature Request: Truncated Distributions for Violin Plots
See original GitHub issueI would like to generate violin plots for truncated distributions, e.g. for efficiency scores which are always between 0 and 100%. My current approach is to use the parameter cut=0
when calling sns.violinplot
, but I think that a more informative approach is to reflect the density at the truncation point, so that, for example, the area which would be drawn below zero in an unrestricted kde will appear above zero in the truncated version.
Here is a little example that illustrates my concern and a potential solution:
import numpy as np, pandas as pd, pymc as pm, matplotlib.pyplot as plt, seaborn as sns
%matplotlib inline
np.random.seed(12345)
df = pd.DataFrame(np.random.normal(size=(10,3)).clip(0,5))
sns.violinplot(data=df)
Note the disturbing non-zero density on negative values. Fixing this with cut=0
looks like this:
sns.violinplot(data=df, cut=0)
No more positive density outside the support of the data. But this truncated normal should have maximum density at zero, and the feature I am requesting is a way to ask for that. Here is a very hacky way to get something that would satisfy me:
t = sns.categorical._ViolinPlotter.fit_kde
def reflected_once_kde(self, x, bw):
kde, bw_used = t(self, x, bw)
kde_evaluate = kde.evaluate
def zero_to_five_truncated_kde_evaluate(x):
val = kde_evaluate(x)
val += kde_evaluate(-x)
val += kde_evaluate(5-(x-5))
return np.where((x<0)|(x>5), 0, val)
kde.evaluate = zero_to_five_truncated_kde_evaluate
return kde, bw_used
sns.categorical._ViolinPlotter.fit_kde = reflected_once_kde
sns.violinplot(data=df, cut=0)
There is a previous feature request that asks for something similar at #244 which was closed when the implementation was overhauled in #410. Perhaps @PierreBdR or @mwaskom has some input about if and how my feature should be implemented.
I am up for doing some amount of work on this if it would be a welcome addition to Seaborn.
Issue Analytics
- State:
- Created 8 years ago
- Comments:12 (5 by maintainers)
Thanks for your work on this. In case anyone needs this sort of plot before the truncated KDE is finished, here is the monkey patch madness that I used in the end:
It made my violins look like gyro meat, which I kind of like:
is there any updates or ways to do this? I would like to have something like the clip option in kdeplot for violinplot so that I don’t have to move to ggplot. Thanks