使用histogram2d python找到平均bin值
发布时间:2020-12-20 11:36:40 所属栏目:Python 来源:网络整理
导读:参见英文答案 binning data in python with scipy/numpy????????????????????????????????????6个 如何在python中使用2D直方图计算容器的平均值?我有x轴和y轴的温度范围,我试图用相应温度的箱子绘制闪电概率.我正在读取csv文件中的数据,我的代码是这样的:
参见英文答案 >
binning data in python with scipy/numpy????????????????????????????????????6个
如何在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() 这样可以产生漂亮的绘图,但绘制的数据是计数或落入每个bin的样本数.验证变量是一个包含1和0的数组,其中1表示闪电,0表示没有闪电.我希望绘图中的数据是基于验证变量数据的给定bin的闪电概率 – 因此我需要bin_mean * 100才能获得此百分比. 我尝试使用类似于此处所示的方法(binning data in python with scipy/numpy),但我很难将其用于2D直方图. 解决方法
这至少可以通过以下方法实现
# 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边缘,则可以通过添加一行来完成相同的直方图部分. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |