Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Density plot for truncated data

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?

like image 670
BayerSe Avatar asked Dec 15 '25 08:12

BayerSe


1 Answers

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

enter image description here

like image 160
tmdavison Avatar answered Dec 16 '25 20:12

tmdavison



Donate For Us

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