Python 每个箱子中点数相等的直方图

Python 每个箱子中点数相等的直方图,python,matplotlib,histogram,Python,Matplotlib,Histogram,我有一个排序向量点,有100个点。现在我想创建两个直方图:第一个直方图应该有10个宽度相等的箱子。第二个也应该有10个直方图,但宽度不一定相等。在第二种情况下,我只希望直方图在每个箱子中有相同数量的点。例如,第一个条可能很短很宽,而直方图中的第二个条可能很高很窄。我有使用matplotlib创建第一个直方图的代码,但现在我不知道如何创建第二个直方图 import matplotlib.pyplot as plt points = [1,2,3,4,5,6, ..., 99] n, bins, p

我有一个排序向量
,有100个点。现在我想创建两个直方图:第一个直方图应该有10个宽度相等的箱子。第二个也应该有10个直方图,但宽度不一定相等。在第二种情况下,我只希望直方图在每个箱子中有相同数量的点。例如,第一个条可能很短很宽,而直方图中的第二个条可能很高很窄。我有使用
matplotlib
创建第一个直方图的代码,但现在我不知道如何创建第二个直方图

import matplotlib.pyplot as plt
points = [1,2,3,4,5,6, ..., 99]
n, bins, patches = plt.hist(points, 10)
编辑:

尝试下面的解决方案,我有点困惑,为什么我的直方图中所有条的高度都是相同的

x = np.random.rand(1000)
mass = np.random.rand(1000)
npoints = 200
ksort = np.argsort(x)

#Here I get the bins from the data set.
#Note that data need to be sorted
bins=x[ksort[0::npoints]]
bins=np.append(bins,x[ksort[-1]])


fig = plt.figure(1,figsize=(10,5))
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)

#Histogram where each data 
yhist, xhist = np.histogram(x, bins, weights=None)
ax1.plot(0.5*(xhist[1:]+xhist[:-1]), yhist, linestyle='steps-mid', lw=2, color='k')

yhist, xhist = np.histogram(x, bins, weights=mass)
ax2.plot(0.5*(xhist[1:]+xhist[:-1]), yhist, linestyle='steps-mid', lw=2, color='k')

ax1.set_xlabel('x', size=15)
ax1.set_ylabel('Number of points per bin', size=15)

ax2.set_xlabel('x', size=15)
ax2.set_ylabel('Mass per bin', size=15)

为柱状图提供箱子:

bin=points[0::len(points)/10]

然后

n,bin,patches=plt.hist(点,bin=bin)

(提供排序的分数)

这个问题是我不久前写的一个答案,但差异很大,足以证明它是自己的问题。事实证明,这个解决方案使用的代码与我的另一个答案基本相同

def histedges_equalN(x, nbin):
    npt = len(x)
    return np.interp(np.linspace(0, npt, nbin + 1),
                     np.arange(npt),
                     np.sort(x))

x = np.random.randn(100)
n, bins, patches = plt.hist(x, histedges_equalN(x, 10))
这个解决方案给出了一个具有相等高度箱子的直方图,因为根据定义,直方图是每个箱子中点数的计数


要获取pdf(即密度函数),请使用
normed=True
kwarg To plt.hist。如我的报告中所述

这里我写了一个例子,说明如何得到结果。我的方法使用数据点来获取将被传递到
np.histogram
构建直方图的箱子。因此需要使用
np.argsort(x)
对数据进行排序。每个箱子的点数可以通过
npoints
控制。作为一个例子,我用这种方法构造了两个直方图。所有点的权重相同,因此直方图的高度始终不变(且等于
npoints
)。另一种方法是,从均匀随机分布中提取每个点的“权重”(参见
质量
阵列)。正如预期的那样,直方图的方框不再相等。然而,每个料仓的泊松误差是相同的

x = np.random.rand(1000)
mass = np.random.rand(1000)
npoints = 200
ksort = np.argsort(x)

#Here I get the bins from the data set.
#Note that data need to be sorted
bins=x[ksort[0::npoints]]
bins=np.append(bins,x[ksort[-1]])


fig = plt.figure(1,figsize=(10,5))
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)

#Histogram where each data 
yhist, xhist = np.histogram(x, bins, weights=None)
ax1.plot(0.5*(xhist[1:]+xhist[:-1]), yhist, linestyle='steps-mid', lw=2, color='k')

yhist, xhist = np.histogram(x, bins, weights=mass)
ax2.plot(0.5*(xhist[1:]+xhist[:-1]), yhist, linestyle='steps-mid', lw=2, color='k')

ax1.set_xlabel('x', size=15)
ax1.set_ylabel('Number of points per bin', size=15)

ax2.set_xlabel('x', size=15)
ax2.set_ylabel('Mass per bin', size=15)

这个解决方案没有那么优雅,但它适合我。希望能有帮助

def pyAC(x, npoints = 10, RetType='abs'):
    x = np.sort(x)
    ksort = np.argsort(x)
    binCount = int(len(x)/npoints) #number of data points in each bin
    bins = np.zeros(npoints) #initialize the bins values
    binsX = np.zeros(npoints)
    for i in range(0, npoints, 1):
        bins[i] = x[(i+1) * binCount]
        for j in range(((binCount * i) + 1), (binCount * (i+1)), 1):
            binsX[i] = x[j] + binsX[i]
    binsX = binsX/binCount  
    return pd.DataFrame({'bins':bins, 'binsX':binsX})

我尝试过使用你的解决方案,但由于某种原因,我所有箱子的高度都是相同的。你知道这是为什么吗?我想他们肯定会有所不同,不是吗?我已经编辑了我的问题以包含一张图片。@Apollo,t该方法有效地改变了bin域的覆盖范围,以实现这一目标。因此,在“垃圾箱”中,你有相同的计数。否则,使用带前缀的箱子大小,一些箱子中会有或多或少的点,但这样每个箱子都有相同的总数;如果您试图在该域上进行集成,可能会出现违反直觉的情况。@farenorth,很好的答案!矢量化和优雅。快速回复
np.historrgram(x,histedges_equalN(x,10))
清楚地向我展示了达到OP(和我)所期望的结果。感觉这确实是一个问题,解决方案是再现np.quantile功能:
np.quantile(x,np.linspace(0,1,nbin+1))
trickOf课程,如果每个箱子包含相同数量的点,则所有条形的高度相同,因为条形的高度是与该箱子相关的点的数量(根据直方图的定义)。查看已接受的答案编辑,其中说明了相同的内容。这几乎有效,但如果步幅不准确;最后一个垃圾箱将丢失。在这种情况下,您也不能仅仅附加它,因为元素的数量不一定接近步幅。例如,如果您有100个元素,并且需要12个箱子,那么您的步幅将为8,并且最终只占100个元素中的97个。如果将最后一个点添加到“bin”,则该bin将仅包含3个元素。