【问题标题】:Plotting interpolated values using LinearNDInterpolator (Python)使用 LinearNDInterpolator (Python) 绘制插值
【发布时间】:2016-03-24 22:01:59
【问题描述】:

我正在使用以下脚本对一些 (x, y, z) 数据使用 LinearNDInterpolator。但是,我无法弄清楚如何从插值数据到以热图形式绘制/显示插值?我是否错过了基于 x 和 y 的最小值和最大值设置网格网格之类的东西?任何帮助或示例都会很棒!

import numpy as np
import scipy.interpolate

x = np.array([-4386795.73911443, -1239996.25110694, -3974316.43669208,
               1560260.49911342,  4977361.53694849, -1996458.01768192,
               5888021.46423068,  2969439.36068243,   562498.56468588,
               4940040.00457585])

y = np.array([ -572081.11495993, -5663387.07621326,  3841976.34982795,
               3761230.61316845,  -942281.80271223,  5414546.28275767,
               1320445.40098735, -4234503.89305636,  4621185.12249923,
               1172328.8107458 ])

z = np.array([ 4579159.6898615 ,  2649940.2481702 ,  3171358.81564312,
               4892740.54647532,  3862475.79651847,  2707177.605241  ,
               2059175.83411223,  3720138.47529587,  4345385.04025412,
               3847493.83999694])

# Create coordinate pairs
cartcoord = zip(x, y)

# Interpolate
interp = scipy.interpolate.LinearNDInterpolator(cartcoord, z)

编辑: 基于@Spinor 的解决方案并使用 Python 2.7,以下代码为我提供了我正在寻找的内容(方法 1)。有没有办法增加插值点的密度?

数据集产生以下图:

不用说,我没想到结果是圆形的,因为 (lat,lon) 坐标取自 equirectrangular 投影图。在进一步调查中,我认为这只是映射到different projection

【问题讨论】:

    标签: python scipy interpolation imaging


    【解决方案1】:

    我假设您正在尝试插入 z 的值。

    现在,当你调用插值函数时会发生什么?它创建了输入(x 和 y)和输出(z)的整个景观。在上面的代码中,您在任何时候都没有真正要求它的值。要使用此功能,您需要指定输入,它会为您提供插值输出。

    您使用了函数 scipy.interpolate.LinearNDInterpolator,它是通过对输入数据进行三角剖分并在每个三角形上执行线性重心插值而构建的。根据您的输入,可能会在某些区域发生故障并获得 Nan。例如,在你的代码中试试这个

    print interp(-4386790, 3720137)
    

    这在 x 和 y 的最小值-最大值范围内。如果您可以接受,我们可以通过 fill_value 参数将 Nan 设置为零。

    阅读文档。通常人们可能会发现以下函数也可以接受,scipy.interpolate.interp2d。它使用样条插值代替。在下面的代码中,我实现了这两个函数(前者的 nan 值设置为 0)并将它们绘制在热图上。

    至于热图,正如您所怀疑的那样。您必须创建一个值网格。下面是我的 LinearNDInterpolator 的输出图,其中 nan 设置为零和 interp2d 以及代码。

    使用 LinearNDInterpolator(cartcoord, z, fill_value=0)

    使用 interp2d(x, y, z)

    附:我正在使用 Python3。如果您在 Python2 中遇到问题,请从 cartcoord = list(zip(x, y)) 中删除该列表。

    import matplotlib.pyplot as plt
    import numpy as np
    import scipy.interpolate
    
    x = np.array([-4386795.73911443, -1239996.25110694, -3974316.43669208,
                   1560260.49911342,  4977361.53694849, -1996458.01768192,
                   5888021.46423068,  2969439.36068243,   562498.56468588,
                   4940040.00457585])
    
    y = np.array([ -572081.11495993, -5663387.07621326,  3841976.34982795,
                   3761230.61316845,  -942281.80271223,  5414546.28275767,
                   1320445.40098735, -4234503.89305636,  4621185.12249923,
                   1172328.8107458 ])
    
    z = np.array([ 4579159.6898615 ,  2649940.2481702 ,  3171358.81564312,
                   4892740.54647532,  3862475.79651847,  2707177.605241  ,
                   2059175.83411223,  3720138.47529587,  4345385.04025412,
                   3847493.83999694])
    
    # Create coordinate pairs
    cartcoord = list(zip(x, y))
    
    
    X = np.linspace(min(x), max(x))
    Y = np.linspace(min(y), max(y))
    X, Y = np.meshgrid(X, Y)
    
    # Approach 1
    interp = scipy.interpolate.LinearNDInterpolator(cartcoord, z, fill_value=0)
    Z0 = interp(X, Y)
    plt.figure()
    plt.pcolormesh(X, Y, Z0)
    plt.colorbar() # Color Bar
    plt.show()
    
    # Approach 2
    func = scipy.interpolate.interp2d(x, y, z)
    Z = func(X[0, :], Y[:, 0])
    plt.figure()
    plt.pcolormesh(X, Y, Z)
    plt.colorbar() # Color Bar
    plt.show()
    

    【讨论】:

    • 很好的解决方案,带有示例和示例代码!谢谢!我正在编辑我的问题以包含基于@Spinor 解决方案的当前结果,因为它是同一主题的扩展。
    • 要增加密度,请将 linspace 函数更改为 X = np.linspace(min(x), max(x), num=100) 并对 Y 执行此操作。
    猜你喜欢
    • 2021-02-18
    • 1970-01-01
    • 2020-03-30
    • 2013-12-29
    • 2020-10-12
    • 2021-05-30
    • 2017-02-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多