我有以下代码从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_85
和min_37
,对应于2D直方图中的概率。我知道有一种蛮力方法,你可以创建一个荒谬的if
语句,每个像素一个,但是当尝试合并多个2D直方图时,这是繁琐且低效的。是否有更有效的方法根据给定的min_85
和min_37
从直方图中引用概率?我有一个单独的文件,其中包含大量风暴的min_85
和min_37
数据,我只需要将相应的闪电概率从直方图中分配给每个风暴。
答案 0 :(得分:1)
听起来您需要做的就是将min_85
和min_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]
但是在提取之前确保结果索引是有效的。