Limit the range of x in seaborn distplot KDE estimation

﹥>﹥吖頭↗ 提交于 2021-02-06 10:45:44

问题


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

Using seaborn, I want to plot a distribution plot:

sns.distplot(arr, hist=False)

Which will give us the following figure:

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

The second line raises an exception -- range not a valid keyword for kde_kws.


回答1:


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

which will produce:

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



回答2:


Settins plt.xlim(0, 1) beforehand should help :

plt.xlim(0, 1)
sns.distplot(arr, hist=False)


来源:https://stackoverflow.com/questions/45911709/limit-the-range-of-x-in-seaborn-distplot-kde-estimation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!