在seaborn distplot KDE估计中限制x的范围 [英] Limit the range of x in seaborn distplot KDE estimation
问题描述
假设我们有一个数字在 0 到 1 之间的数组:
Suppose we have an array with numbers between 0 and 1:
arr=np.array([ 0. , 0. , 0. , 0. , 0.6934264 ,
0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0.6934264 , 0. , 0.6934264 ,
0. , 0. , 0. , 0. , 0.251463 ,
0. , 0. , 0. , 0.87104906, 0.251463 ,
0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. , 0.48419626,
0. , 0. , 0. , 0. , 0. ,
0.87104906, 0. , 0. , 0.251463 , 0.48419626,
0. , 0.251463 , 0. , 0. , 0. ,
0. , 0. , 0. , 0. , 0. ,
0. , 0.251463 , 0. , 0.35524532, 0. ,
0. , 0. , 0. , 0. , 0.251463 ,
0.251463 , 0. , 0.74209813, 0. , 0. ])
使用seaborn
,我想绘制一个分布图:
Using seaborn
, I want to plot a distribution plot:
sns.distplot(arr, hist=False)
这将为我们提供下图:
如您所见,kde 估计值范围从接近 -0.20 到 1.10.是否可以强制估计在 0 和 1 之间?我尝试了以下方法但没有成功:
As you can see, the kde estimation ranges from somewhere near -0.20 to 1.10. Is it possible to force the estimation to be between 0 and 1? I have tried the followings with no luck:
sns.distplot(arr, hist=False, hist_kws={'range': (0.0, 1.0)})
sns.distplot(arr, hist=False, kde_kws={'range': (0.0, 1.0)})
第二行引发异常 -- range
不是 kde_kws
的有效关键字.
The second line raises an exception -- range
not a valid keyword for kde_kws
.
推荐答案
正确的做法是使用 clip
关键字而不是 range
:>
The correct way of doing this, is by using the clip
keyword instead of range
:
sns.distplot(arr, hist=False, kde_kws={'clip': (0.0, 1.0)})
这将产生:
确实,如果你只关心 kde 而不是直方图,你可以使用 kdeplot
函数,它会产生相同的结果:
Indeed, if you only care about the kde and not the histogram, you can use the kdeplot
function, which will produce the same result:
sns.kdeplot(arr, clip=(0.0, 1.0))
这篇关于在seaborn distplot KDE估计中限制x的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!