[英]line interpolation on grid with python3
I am very new to python(say 1 week) and numpy/scipy but not new to programming at all but wondering how to do the following correctly (preferably with numpy/scipy): 我对python(例如1周)和numpy / scipy非常陌生,但对编程一点都不陌生,但想知道如何正确执行以下操作(最好使用numpy / scipy):
So say I have an 150x200 ndarray with float values. 所以说我有一个150x200的ndarray带有float值。 I want to interpolate a line from 20:40 to 100:150 with 500 points in between.
我想在20:40到100:150之间插入500点之间的直线。
Getting the x:y interpolation I have with: 得到我的x:y插值:
xValues = numpy.linspace(20,100,500)
yValues = numpy.linspace(40,150,500)
But now how i get the values(on that line only) interpolated using numpy/scipy? 但是现在我如何使用numpy / scipy插值(仅在该行)?
PS I use python3 PS我使用python3
Check out Scipy.interpolate 签出Scipy.interpolate
import numpy as np
from scipy.interpolate import interp1d
xValues = numpy.linspace(20,100,500)
yValues = numpy.linspace(40,150,500)
f = interpolate.interp1d(x, y)
xnew = np.arange(20, 30, 0.1)
ynew = f(xnew)
I currently did it like this: 我目前这样做是这样的:
I am wondering if I do this correctly: 我想知道我是否正确地做到了:
def get_interpolated_slice(self, grid_start_x, grid_start_y, grid_end_x, grid_end_y, resolution=100):
x = np.arange(self.z_data.shape[1])
y = np.arange(self.z_data.shape[0])
interpolated_z_ft = interpolate.interp2d(x, y, self.z_data)
xvalues = np.linspace(grid_start_x, grid_end_x, resolution)
yvalues = np.linspace(grid_start_y, grid_end_y, resolution)
interpolated_y_values = interpolated_y_m(xvalues, yvalues)
z_to_return = [None] * resolution
for i in range(resolution):
z_to_return[i] = interpolated_z_values[i][i]
return z_to_return
So what do I do: I interpolate the 2d array(matrix) completely for a range between 2 points. 所以我该怎么办:我将2d数组(矩阵)完全插值到2点之间的范围内。 Since it will be a resolution X resolution grid I can make a diagonal 'walk' through the matrix that will result in the a line.
由于它将是一个分辨率为X的分辨率网格,因此我可以在矩阵中进行对角线“遍历”,从而形成一条直线。
Can someone confirm if this is correct and/or this is the correct way to do it? 有人可以确认这是否正确和/或这是正确的方法吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.