How do you calculate averages for bins with a 2D histogram in python? I have temperature ranges for the x and y axis, and I'm trying to build the probability of lightning using bins for the corresponding temperatures. I am reading data from a csv file and my code is this:
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()
This makes a nice looking plot, but the data that is built is the number or number of samples that fall into each bit. The verification variable is an array containing 1 and 0, where 1 indicates lightning and 0 indicates the absence of lightning. I want the data on the chart to be a lightning probability for a given bin based on the data from the verification variable, so I need bin_mean * 100 to get this percentage.
I tried using an approach similar to what is shown here ( binning data in python with scipy / numpy ), but it was difficult for me to get it working on a 2D histogram.
python numpy scipy matplotlib
mbreezy Jul 23 '14 at 18:03 2014-07-23 18:03
source share