我试图在网格中的一组点上绘制一条直线.数据位于x,y,z坐标列表中.我认为map_coordinates是我想要的,但是我没有看到输入和输出的形式......任何帮助都将非常感激.
list = [[x1, y1, z1]
[x2, y2, z2]
...
[xn, yn, zn]]
Run Code Online (Sandbox Code Playgroud)
我想要查找的值是x和y值.
look_up_values= [[x1, y1]
[x2, y2]
...
[xn, yn]]
Run Code Online (Sandbox Code Playgroud)
我的问题是,为什么map_coordinates期望一个(2,2)数组和输出中的信息(2值列表).
这是一个可行的例子:
from scipy.ndimage.interpolation import map_coordinates
import numpy as np
in_data = np.array([[0.,0.,0.]
,[0.,1.,.2]
,[0.,2.,.4]
,[1.,0.,.2]
,[1.,3.,.5]
,[2.,2.,.7]])
z = map_coordinates(in_data, np.array([[1.,1.],[1.,2.]]), order=1)
print z #I do not understand this output...
#[1. .2]
Run Code Online (Sandbox Code Playgroud)
如果我不得不猜测我会说它在网格上的2点之间插值,那么买输出意味着什么呢?
输出map_coordinates是在您指定的坐标处插入原始数组的值.
在您输入的示例中(1,1), (1,2).这意味着您需要两个位置的插值:点x = 1,y = 1和x = 1,y = 2.它需要两个数组,因为每个数组都是x和y坐标.即你有两个坐标要求:x坐标为1,1,y坐标为1,2.
您的输入可以是您想要的长或短,但阵列必须长度相同,因为它们是耦合的.
让我尝试回答首先检查一步一步的 1d 插值情况:
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d
import numpy as np
### 1d example of interpolation ###
in_data_x = np.array([1., 2., 3., 4., 5., 6.])
in_data_y = np.array([1.5, 2., 2.5, 3., 3.5, 4.]) # y = .5 x - 1
f = interp1d(in_data_x, in_data_y, kind='linear')
print(f)
# f in all of the points of the grid (in_data_x): output coincides with in_data_y
print(f(1), f(1.), f(1.5), f(2.), f(2.5), f(3.))
# f in a point outside the grid:
print(f(1.8))
# this is equal to y = .5 x - 1 for x = 1.8, up to some point.
assert round(0.5 * 1.8 + 1, ndigits=10) == round(f(1.8), ndigits=10)
# plot up to this point
xnew = np.arange(1, 6, 0.1)
ynew = f(xnew)
plt.plot(in_data_x, in_data_y, 'o', xnew, ynew, '-')
# close the image to move forward.
plt.show()
### another 1d example of interpolation ###
in_data_x = np.array([1., 2., 3., 4., 5., 6.])
in_data_y = np.array([-1.8, -1.2, -0.2, 1.2, 3., 5.2]) # y = .2 x**2 - 2
f = interp1d(in_data_x, in_data_y, kind='cubic')
print(f)
# f in all of the points of the grid (in_data_x): output coincides with in_data_y
print(f(1), f(1.), f(1.5), f(2.), f(2.5), f(3.))
# f in a point outside the grid:
print(f(1.8))
# this is equal to y = .2 x**2 - 2 for x = 1.8, up to some precision.
assert round(0.2 * 1.8 ** 2 - 2, ndigits=10) == round(f(1.8), ndigits=10)
# plot up to this point
xnew = np.arange(1, 6, 0.1)
ynew = f(xnew)
plt.plot(in_data_x, in_data_y, 'o', xnew, ynew, '-')
plt.show()
Run Code Online (Sandbox Code Playgroud)
函数 interp1d 为您提供了一个插值器,该插值器为您提供使用某种算法(在本例中为线性)内插的值,该函数通过 x = [1., 2., 3., 4., 5., 6.] y = [-1.8, -1.2, -0.2, 1.2, 3., 5.2]。
map_coordinates 也是如此。当您的数据具有多个维度时。第一个主要区别是结果不是插值器,而是一个数组。第二个主要区别是 x 坐标由数据维度的矩阵坐标给出。第三个区别是输入必须作为列向量给出。看这个例子
from scipy.ndimage.interpolation import map_coordinates
import numpy as np
in_data = np.array([[0., -1., 2.],
[2., 1., 0.],
[4., 3., 2.]]) # z = 2.*x - 1.*y
# want the second argument as a column vector (or a transposed row)
# see on some points of the grid:
print('at the point 0, 0 of the grid the function z is: ')
print(map_coordinates(in_data, np.array([[0., 0.]]).T, order=1))
print('at the point 0, 1 of the grid the function z is: ')
print(map_coordinates(in_data, np.array([[0., 1.]]).T, order=1))
print('at the point 0, 2 of the grid the function z is: ')
print(map_coordinates(in_data, np.array([[0., 2.]]).T, order=1))
# see some points outside the grid
print()
print('at the point 0.2, 0.2 of the grid, with linear interpolation z is:')
print(map_coordinates(in_data, np.array([[.2, .2]]).T, order=1))
print('and it coincides with 2.*.2 - .2')
print()
print('at the point 0.2, 0.2 of the grid, with cubic interpolation z is:')
print(map_coordinates(in_data, np.array([[0.2, .2]]).T, order=3)
Run Code Online (Sandbox Code Playgroud)
终于回答了你的问题,你给出了输入
in_data = np.array([[0., 0., 0.],
[0., 1., .2],
[0., 2., .4],
[1., 0., .2],
[1., 3., .5],
[2., 2., .7]])
Run Code Online (Sandbox Code Playgroud)
这是在由矩阵坐标给出的网格上计算的函数 z(x,y): z(0, 0) = 0. z(2, 2) = .7
z = map_coordinates(in_data, np.array([[1., 1.], [1., 2.]]), order=1)
Run Code Online (Sandbox Code Playgroud)
意味着询问 z(1,1) 和 z(1,2),其中第二个输入数组是按列读取的。
z = map_coordinates(in_data, np.array([[.5, .5]]).T, order=1)
Run Code Online (Sandbox Code Playgroud)
意味着询问 z(0.5, 0.5)。注意输入中的转置 .T。希望它确实有意义并且有帮助。