bin 3d指向python中的3d bins

Noa*_*led 5 python multidimensional-array binning

如何将 3d 点放入 3d 箱中?np.digitize 是否有多维版本?我可以为每个维度分别使用 np.digitize ,就像这里。有更好的解决方案吗?谢谢!

Ed *_*ith 7

您可以使用 来做到这一点numpy.histogramdd(sample),其中每个方向的 bin 数量和物理范围可以像一维直方图一样进行调整。参考上的更多信息。对于更一般的统计数据,例如 bin 中每个点的另一个变量的平均值,您可以使用 scipyscipy.stats.binned_statistic_dd函数,请参阅docs。对于具有三维点数组的情况,您可以按以下方式使用它,

import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from scipy import stats


#Setup some dummy data
points = np.random.randn(1000,3)
hist, binedges = np.histogramdd(points, normed=False)

#Setup a 3D figure and plot points as well as a series of slices
fig = plt.figure()
ax1 = fig.add_subplot(111, projection='3d')
ax1.plot(points[:,0],points[:,1],points[:,2],'k.',alpha=0.3)

#Use one less than bin edges to give rough bin location
X, Y = np.meshgrid(binedges[0][:-1],binedges[1][:-1])

#Loop over range of slice locations (default histogram uses 10 bins)
for ct in [0,2,5,7,9]: 
    cs = ax1.contourf(X,Y,hist[:,:,ct], 
                      zdir='z', 
                      offset=binedges[2][ct], 
                      level=100, 
                      cmap=plt.cm.RdYlBu_r, 
                      alpha=0.5)

ax1.set_xlim(-3, 3)
ax1.set_ylim(-3, 3)
ax1.set_zlim(-3, 3)
plt.colorbar(cs)
plt.show()
Run Code Online (Sandbox Code Playgroud)

它给出了每个位置的一系列占用率直方图切片,

在此处输入图片说明