基本的に、ライン (または任意のパス) に沿って 2D グリッドを補間します。
まず、グリッドを補間するか、最近傍サンプリングを行うかを決定する必要があります。後者を行いたい場合は、インデックス作成を使用できます。
補間したい場合は、 をご覧くださいscipy.ndimage.map_coordinates
。最初は頭を包み込むのが少し難しいですが、これは完璧です。(データ ポイントがランダムに分布していると仮定する補間ルーチンを使用するよりもはるかに効率的です。)
両方の例を挙げます。これらは、私が別の質問に答えたものを基にしています。 ただし、これらの例では、すべてが「ピクセル」(行、列) 座標でプロットされます。
あなたの場合、「ピクセル」座標とは異なる座標系で作業しているため、補間のために「ワールド」(x、y) 座標から「ピクセル」座標に変換する必要があります。
まず、3 次補間を使用する例を次に示しますmap_coordinates
。
import numpy as np
import scipy.ndimage
import matplotlib.pyplot as plt
# Generate some data...
x, y = np.mgrid[-5:5:0.1, -5:5:0.1]
z = np.sqrt(x**2 + y**2) + np.sin(x**2 + y**2)
# Coordinates of the line we'd like to sample along
line = [(-3, -1), (4, 3)]
# Convert the line to pixel/index coordinates
x_world, y_world = np.array(zip(*line))
col = z.shape[1] * (x_world - x.min()) / x.ptp()
row = z.shape[0] * (y_world - y.min()) / y.ptp()
# Interpolate the line at "num" points...
num = 1000
row, col = [np.linspace(item[0], item[1], num) for item in [row, col]]
# Extract the values along the line, using cubic interpolation
zi = scipy.ndimage.map_coordinates(z, np.vstack((row, col)))
# Plot...
fig, axes = plt.subplots(nrows=2)
axes[0].pcolormesh(x, y, z)
axes[0].plot(x_world, y_world, 'ro-')
axes[0].axis('image')
axes[1].plot(zi)
plt.show()

または、最近傍補間を使用することもできます。これを行う 1 つの方法は、上記の例でに渡すorder=0
ことです。map_coordinates
代わりに、インデックス作成を使用して、別のアプローチを示します。行を変更するだけなら
# Extract the values along the line, using cubic interpolation
zi = scipy.ndimage.map_coordinates(z, np.vstack((row, col)))
に:
# Extract the values along the line, using nearest-neighbor interpolation
zi = z[row.astype(int), col.astype(int)]
得られるもの:
