Python 如何从seaborn';s KDE distplot对象? 背景

Python 如何从seaborn';s KDE distplot对象? 背景,python,grouping,histogram,seaborn,discrete-mathematics,Python,Grouping,Histogram,Seaborn,Discrete Mathematics,我试图根据我掌握的一种植物(拟南芥)在光照下基因变化的数据,对11000个基因进行分组 每个基因的原始值是连续的随机变量,但我希望离散化这些值,比如说,有20个离散类 因此,不是: change_in_expression = array([-2.2, -1.1, -1.2, ..., 0.6, -1. , -0.9]) 我有课堂产出: change_in_expression = array(["3_to_4","2_to_3","1_to

我试图根据我掌握的一种植物(拟南芥)在光照下基因变化的数据,对11000个基因进行分组

每个基因的原始值是连续的随机变量,但我希望离散化这些值,比如说,有20个离散类

因此,不是:

change_in_expression = array([-2.2, -1.1, -1.2, ...,  0.6, -1. , -0.9])
我有课堂产出:

change_in_expression = array(["3_to_4","2_to_3","1_to_2",...])

我试过的 我使用
seaborn的distplot()
绘制分布图,我相信它使用了:

我知道当默认设置“自动”生成这些分组时,
matplotlib.pyplot的
hist()
允许您提取箱子


摘要问题 问题是,我如何将我的基因分组?这是一个比仅仅询问
seaborn
版本的
matplotlib的
hist()
更广泛的问题。。。因为
seaborn的
distplot
使用KDE

通过查看以下可用方法,我似乎无法从seaborn创建的
ax
对象中获取垃圾箱:

dir(sns.distplot(d, fit=stats.laplace, kde=False)

我想,一种方法是检查seaborn的
distplot
源代码的内部,找出它们在绘图之前是如何存储数据的。。。但这远远超出了我的独角兽技能…

Seaborn调用
pyplot.hist
,它反过来调用
numpy.histogram
。因此,如果未指定任何参数,可以检查seaborn将什么用作
bin
参数。即,
a
作为数据

bins = min(_freedman_diaconis_bins(a), 50)
在哪里

所有这些都应该大致与

bins = min(len(numpy.histogram_bin_edges(a, bins="fd")), 50)

然后将
bin
传递到
pyplot.hist

plt.hist(a, bins=bins)

distplot是一种柱状图,顶部有kde的图。这些是独立的。由于KDE没有“分组”,我不确定这个问题的真正目的是什么?@ImportanceOfBeingErnest,谢谢你的编辑。还有你更深层次的理解:我想问题是,什么算法决定了装箱,我想我可以四处寻找源代码的直方图部分。谢谢你,朋友!很明显,你已经超越了责任的召唤,你自己寻找源头,这是令人惊讶的。。。作为Stack的新手,我真不敢相信你们会有多大帮助。我现在已经研究了freedman diaconis并了解了IQR的依赖性。我试着+1,但我还是
def iqr(a):
    """Calculate the IQR for an array of numbers."""
    a = np.asarray(a)
    q1 = stats.scoreatpercentile(a, 25)
    q3 = stats.scoreatpercentile(a, 75)
    return q3 - q1
bins = min(len(numpy.histogram_bin_edges(a, bins="fd")), 50)
bins = 50 if len(numpy.histogram_bin_edges(a, bins="fd")) > 50 else "fd"
plt.hist(a, bins=bins)