小编典典

使用LinearNDInterpolator(Python)绘制插值

python

我使用以下脚本在一些(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)。有没有办法增加插值点的密度?

数据集产生以下图:
在此处输入图片说明

不用说,我没想到结果是圆形的,因为(纬度,经度)坐标取自等角投影图。在进一步调查中,我认为这只是映射到不同的投影上


阅读 535

收藏
2021-01-20

共1个答案

小编典典

我将假定您正在尝试对z的值进行插值。

现在,当您调用插值函数时会发生什么?它创建输入(x和y)和输出(z)的整个格局。在上面的代码中,您实际上并没有真正要求它的值。要使用此功能,您需要指定输入,它将为您提供插值输出。

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

print interp(-4386790, 3720137)

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

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

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

使用LinearNDInterpolator(cartcoord,z,fill_value = 0)
在此处输入图片说明

使用interp2d(x,y,z)
在此处输入图片说明

PS我正在使用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()
2021-01-20