2014-09-10 82 views
1

我有下面的代碼,從一個CSV文件中讀取數據,並創建一個二維直方圖:引用數據從2D直方圖

import numpy as np 
import pandas as pd 
import matplotlib as mpl 
import matplotlib.pyplot as plt 

#Read in CSV data 
filename = 'Complete_Storms_All_US_Only.csv' 
df = pd.read_csv(filename) 

min_85 = df.min85 
min_37 = df.min37 
verification = df.one_min_15 

#Numbers 
x = min_85 
y = min_37 
H = verification 

#Estimate the 2D histogram 
nbins = 33 
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) 

#Calculate Averages 
avgarr = np.zeros((nbins, nbins)) 
xbins = np.digitize(x, xedges[1:-1]) 
ybins = np.digitize(y, yedges[1:-1]) 
for xb, yb, v in zip(xbins, ybins, verification): 
    avgarr[yb, xb] += v 
divisor = H.copy() 
divisor[divisor==0.0] = np.nan 
avgarr /= divisor 
binavg = np.around((avgarr * 100), decimals=1) 
binper = np.ma.array(binavg, mask=np.isnan(binavg)) 

#Plot 2D histogram using pcolor 
fig1 = plt.figure() 
plt.pcolormesh(xedges,yedges,binper) 
plt.title('1 minute at +/- 0.15 degrees') 
plt.xlabel('min 85 GHz PCT (K)') 
plt.ylabel('min 37 GHz PCT (K)') 
cbar = plt.colorbar() 
cbar.ax.set_ylabel('Probability of CG Lightning (%)') 

plt.show() 

在直方圖的每個像素包含用於溫度的給定範圍閃電的概率在x和y軸上有兩個不同的頻率(x軸上爲min_85,y軸上爲min_37)。我試圖從直方圖中根據各種不同的溫度,針對任何給定的風暴,根據個人的基礎而提供閃電的概率。每個風暴有一個min_85min_37,對應於來自2D直方圖的概率。我知道有一種蠻力方法,您可以創建一個荒謬的數量的if語句,每個像素有一個,但是當嘗試合併到多個2D直方圖時,這是單調乏味且效率低下的。根據給定的min_85min_37,有沒有更有效的方法來從直方圖中引用概率?我有一個單獨的文件,其中包含針對大量風暴的min_85min_37數據,我只需要將直方圖中相應的閃電概率分配給每個數據。

回答

0

聽起來你所需要做的就是將min_85min_37的值轉換爲索引。像這樣的東西將工作:

# min85data and min37data from your file 
dx = xedges[1] - xedges[0] 
dy = yedges[1] - yedges[0] 
min85inds = np.floor((min85data - yedges[1])/dx).astype(np.int) 
min37inds = np.floor((min37data - yedges[0])/dy).astype(np.int) 

# Pretend you didn't do all that flipping of H, or make a copy of it first 
hvals = h_orig[min85inds, min37ends] 

但是請確保所得的指數是有效的,然後再提取它們。

+0

感謝您的幫助,這很好! – mbreezy 2014-09-10 18:48:27