I need to draw the density of a truncated variable. The data can be either bounded from below or from both sides. For example,
import numpy as np
import seaborn as sns
data = np.random.normal(size=1000000)
data[data < 0] = 0
data[data > 1] = 1
sns.kdeplot(data)
How can I draw the density such that there is no probability mass outside the boundaries?
Are you looking for the clip kwarg?
From the docs for kdeplot:
clip : pair of scalars, or pair of pair of scalars, optional
Lower and upper bounds for datapoints used to fit KDE. Can provide a pair of (low, high) bounds for bivariate plots.
import numpy as np
import seaborn as sns
data = np.random.normal(size=1000000)
lower,upper = 0,1
sns.kdeplot(data,clip=(lower,upper))

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With