境界がポリゴン頂点のリストとして与えられていると仮定すると、matplotlib でデータ座標に対してマスクを生成し、そのマスクを使用して等高線内の値のみを合計することができます。
つまり、関心領域をマークする多角形の境界を定義する一連の座標がある場合、matplotlib に、この多角形内にあるすべての座標を示すブール値マスクを生成させます。このマスクを使用して、等高線内の限られた降雨量のデータセットのみを抽出できます。
次の簡単な例は、これがどのように行われるかを示しています。
import numpy as np
from matplotlib.patches import PathPatch
from matplotlib.path import Path
import matplotlib.pyplot as plt
# generate some fake data
xmin, xmax, ymin, ymax = -10, 30, -4, 20
y,x = np.mgrid[ymin:ymax+1,xmin:xmax+1]
z = (x-(xmin+xmax)/2)**2 + (y-(ymin + ymax)/2)**2
extent = [xmin-.5, xmax+.5, ymin-.5, ymax+.5]
xr, yr = [np.random.random_integers(lo, hi, 3) for lo, hi
in ((xmin, xmax), (ymin, ymax))] # create a contour
coordlist = np.vstack((xr, yr)).T # create an Nx2 array of coordinates
coord_map = np.vstack((x.flatten(), y.flatten())).T # create an Mx2 array listing all the coordinates in field
polypath = Path(coordlist)
mask = polypath.contains_points(coord_map).reshape(x.shape) # have mpl figure out which coords are within the contour
f, ax = plt.subplots(1,1)
ax.imshow(z, extent=extent, interpolation='none', origin='lower', cmap='hot')
ax.imshow(mask, interpolation='none', extent=extent, origin='lower', alpha=.5, cmap='gray')
patch = PathPatch(polypath, facecolor='g', alpha=.5)
ax.add_patch(patch)
plt.show(block=False)
print(z[mask].sum()) # prints out the total accumulated
この例では、x
andy
は yourUTM-X
およびUTM-Y
dataranges を表します。z
は気象降雨量データを表しますが、この場合はマトリックスであり、平均降雨量の単一列ビュー (グリッドに簡単に再マッピングできます) とは異なります。
z
最後の行では、輪郭内にあるすべての値を合計しました。平均が必要な場合は、 に置き換えsum
てmean
ください。