【问题标题】:line interpolation on grid with python3使用python3在网格上进行线插值
【发布时间】:2016-01-27 15:11:06
【问题描述】:

我对 python(比如 1 周)和 numpy/scipy 非常陌生,但对编程一点也不陌生,但想知道如何正确执行以下操作(最好使用 numpy/scipy):

假设我有一个带有浮点值的 150x200 ndarray。我想在 20:40 到 100:150 之间插入一条线,中间有 500 个点。

得到 x:y 插值:

xValues = numpy.linspace(20,100,500)
yValues = numpy.linspace(40,150,500)

但是现在我如何获得使用 numpy/scipy 插值的值(仅在该行上)?

PS 我用的是python3

【问题讨论】:

  • 因此产生一个包含 500 个值的一维数组

标签: python python-3.x numpy scipy


【解决方案1】:

查看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)

【讨论】:

  • 您好,您的意思是 x=xValues 和 y=yValues?
【解决方案2】:

我目前是这样做的:

我想知道我这样做是否正确:

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

那我该怎么办: 我在 2 点之间的范围内完全插入 2d 数组(矩阵)。由于它将是分辨率 X 分辨率网格,因此我可以在矩阵中进行对角线“行走”,从而产生一条线。

有人可以确认这是否正确和/或这是正确的方法吗?

【讨论】:

    猜你喜欢
    • 2018-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-21
    • 1970-01-01
    • 1970-01-01
    • 2021-01-05
    • 2012-12-16
    相关资源
    最近更新 更多