Basically, you want to interpolate a 2D mesh along a line (or an arbitrary path).
First of all, you must decide whether you want to interpolate the grid or just select the nearest neighbors. If you want to do the latter, you can simply use indexing.
If you want to interpolate, look at scipy.ndimage.map_coordinates
. At first itβs a little difficult for you to wrap your head, but it is perfect for this. (This is much more efficient than using an interpolation procedure, assuming data points are distributed randomly.)
I will give an example of both. They are adapted from the answer that I gave to another question. However, in these examples, everything is built in pixel coordinates (ie, row, column).
In your case, you are working in a different coordinate system than the coordinates of the "pixel", so you will need to convert from the coordinates of the "world" (ie x, y) to the "pixel" coordinates for interpolation.
Firstly, here is an example of using cubic interpolation with map_coordinates
:
import numpy as np import scipy.ndimage import matplotlib.pyplot as plt
Alternatively, we can use interpolation of the nearest neighbor. One way to do this is to pass order=0
to map_coordinates
in the example above. Instead, I will use indexing to show a different approach. If we just change the line
To:
We'll get:
source share