mbr*_*ezy 3 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()
Run Code Online (Sandbox Code Playgroud)
这样可以产生漂亮的绘图,但绘制的数据是计数或落入每个bin的样本数.验证变量是一个包含1和0的数组,其中1表示闪电,0表示没有闪电.我希望绘图中的数据是基于验证变量数据的给定bin的闪电概率 - 因此我需要bin_mean*100才能获得此百分比.
我尝试使用类似于此处所示的方法(在python中使用scipy/numpy对数据进行分箱),但是我很难将其用于2D直方图.
有一种优雅而快速的方法来做到这一点!使用weights参数来汇总值:
denominator, xedges, yedges = np.histogram2d(x,y,bins=nbins)
nominator, _, _ = np.histogram2d(x,y,bins=[xedges, yedges], weights=verification)
Run Code Online (Sandbox Code Playgroud)
所以你需要的是在每个bin中将值的总和除以事件的数量:
result = nominator / denominator
Run Code Online (Sandbox Code Playgroud)
瞧!
这至少可以通过以下方法实现
# 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)
Run Code Online (Sandbox Code Playgroud)
如果您事先知道箱边缘,则只需添加一行即可以相同的方式完成直方图部分。
| 归档时间: |
|
| 查看次数: |
4190 次 |
| 最近记录: |