I am trying to get only non-negative values on the x-axis on the plot for my KDE. I know I can limit the x-axis values but I do not want that. Is there way to smoothly approximate the KDE such that there are no non-negative value? All my data are non-negative but I do not have a lot of sample points(max 500 and I cannot get more). I have also tried to adjust the bandwidth and its not looking nice.
for i in range(len(B)):
ax = sns.kdeplot(data[i],shade=True)
ax.set_xlabel('Maimum detection time')
ax.legend(['N=25,R=20', 'N=30,R=20', 'N=35,R=20'],fontsize=5)
plt.show()
What goes on behind kdeplot is that a kernel density is fitted with many little normal density (see this illustration) and the densities at the very edge of the truncation cutoff spill over.
Using an example data:
If you use
clip=
, it doesn't stop the evaluation at negative values:If you add
cut=0
, it will look odd. As you pointed out, you can truncate it at 0:There are two solutions proposed in this post on cross-validated. I write a python implementation of the R code provided by @whuber:
We can check how it looks for
data['a']
:You can plot it for both: