使用LinearNDInterpolator(Python)绘制内插值 [英] Plotting interpolated values using LinearNDInterpolator (Python)

查看:447
本文介绍了使用LinearNDInterpolator(Python)绘制内插值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下脚本在某些(x,y,z)数据上使用LinearNDInterpolator.但是,我不知道如何从插值数据转到以热图形式绘制/显示插值?我是否缺少基于x和y的最小值和最大值设置网格网格的内容?任何帮助或示例都将很棒!

I am using the LinearNDInterpolator on some (x, y, z) data, using the following script. However, I cannot figure out how to go from the interpolated data to plotting/showing the interpolation in heatmap form? Am I missing something like setting up a meshgrid based on the min and max of x and y? Any help or an example would be great!

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).有没有办法增加插值点的密度?

Based on @Spinor's solution, and using Python 2.7, the following code gives me what I'm looking for (approach 1). Is there a way to increase my density of the interpolated points?

数据集产生以下图:

The dataset yields the following plot:

不用说,我没想到结果是圆形的,因为(lat,lon)坐标取自等角投影图.经过进一步调查,我认为这只是映射到不同的投影.

Needless to say, I did not expect the results to be circular, since the (lat,lon) coordinates are taken from an equirectrangular projection map. On further investigation, I think this is simply mapped on a different projection.

推荐答案

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

I will assume that you are trying to interpolate values of z.

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

Now, what happens when you do call interpolation function? It creates the entire landscape of the inputs (x and y) and the outputs (z). In the code above, you didn't really ask for its value at any point. To use this function, you need to specify the inputs and it will give you the interpolated output.

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

You used the function scipy.interpolate.LinearNDInterpolator which is constructed by triangulating the input data and on each triangle performing linear barycentric interpolation. Depending on your inputs, there are likely to be regions where this breaks down and you get Nan. For instance, try this in your code

print interp(-4386790, 3720137)

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

This is within the limits of the min-max of your x and y. We could set the Nan to zero via the fill_value argument if that is acceptable to you.

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

Read up on the docs. Often people might find the following function acceptable as well, scipy.interpolate.interp2d. It uses spline interpolation instead. In the code below, I've implemented both functions (the former with nan values set to 0) and plotted them on a heatmap.

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

As for the heatmap, it is as you've suspected. You have to create a grid of values. Below is my the output graphs for LinearNDInterpolator with nan set to zero and interp2d as well as the codes.

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

使用interp2d(x,y,z)

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

P.S. I am using Python3. If you run into issues in Python2, remove the list from 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()

这篇关于使用LinearNDInterpolator(Python)绘制内插值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆