使用histogram2d python查找平均bin值

时间:2014-07-23 18:03:01

标签: python numpy matplotlib scipy

如何在python中使用2D直方图计算容器的平均值?我有x轴和y轴的温度范围,我试图用相应温度的箱子绘制闪电概率。我正在读取csv文件中的数据,我的代码是这样的:

filename = 'Random_Events_All_Sorted_85GHz.csv'
df = pd.read_csv(filename)

min37 = df.min37
min85 = df.min85
verification = df.five_min_1

#Numbers
x = min85
y = min37
H = verification

#Estimate the 2D histogram
nbins = 4
H, xedges, yedges = np.histogram2d(x,y,bins=nbins)

#Rotate and flip H
H = np.rot90(H) 
H = np.flipud(H)

#Mask zeros
Hmasked = np.ma.masked_where(H==0,H)

#Plot 2D histogram using pcolor
fig1 = plt.figure()
plt.pcolormesh(xedges,yedges,Hmasked)
plt.xlabel('min 85 GHz PCT (K)')
plt.ylabel('min 37 GHz PCT (K)')
cbar = plt.colorbar()
cbar.ax.set_ylabel('Probability of Lightning (%)')

plt.show()

这会产生漂亮的情节,但绘制的数据是计数或落入每个容器的样本数。验证变量是一个包含1和0的数组,其中1表示闪电,0表示没有闪电。我希望绘图中的数据是基于验证变量数据的给定bin的闪电概率 - 因此我需要bin_mean * 100才能获得此百分比。

我尝试使用类似于此处显示的方法(binning data in python with scipy/numpy),但我很难将其用于2D直方图。

2 个答案:

答案 0 :(得分:6)

有一种优雅而快速的方法可以做到这一点!使用weights参数对值进行求和:

denominator, xedges, yedges = np.histogram2d(x,y,bins=nbins)
nominator, _, _ = np.histogram2d(x,y,bins=[xedges, yedges], weights=verification)

所以你需要的是在每个bin中将值的总和除以事件的数量:

result = nominator / denominator

瞧!

答案 1 :(得分:1)

这至少可以通过以下方法实现

# xedges, yedges as returned by 'histogram2d'

# create an array for the output quantities
avgarr = np.zeros((nbins, nbins))

# determine the X and Y bins each sample coordinate belongs to
xbins = np.digitize(x, xedges[1:-1])
ybins = np.digitize(y, yedges[1:-1])

# calculate the bin sums (note, if you have very many samples, this is more
# effective by using 'bincount', but it requires some index arithmetics
for xb, yb, v in zip(xbins, ybins, verification):
    avgarr[yb, xb] += v

# replace 0s in H by NaNs (remove divide-by-zero complaints)
# if you do not have any further use for H after plotting, the
# copy operation is unnecessary, and this will the also take care
# of the masking (NaNs are plotted transparent)
divisor = H.copy()
divisor[divisor==0.0] = np.nan

# calculate the average
avgarr /= divisor

# now 'avgarr' contains the averages (NaNs for no-sample bins)

如果您事先知道了bin边缘,只需添加一行就可以完成相同的直方图部分。