2014-02-06 76 views
2

我不知道我在做什么错。我试图使用scipy griddata在不规则网格中插入数据。Scipy Griddata输出尺寸

from scipy.interpolate import griddata 

我有两个列表“x”和“y”,它们表示我的原始非插值网格的坐标轴。它们都是长度为8的列表。

然后,我制作代表预期最终填充网格坐标轴的数组。

ny = np.linspace(0.0, max(y), y[len(y)-1]/min_interval+1) 
nx = np.linspace(0.0, max(x), len(ny)) 

我检查过,“ny”和“nx”的形状都是(61,)。然后,我创建一个8 x 8列表“z”。最后,我试图让我的最终网格。

Z = griddata((np.array(x), np.array(y)), np.array(z), (nx, ny), method='nearest', fill_value=0) 
print Z.shape 

得到的二维数组具有尺寸(61,8)。我尝试使用“x”和“y”作为列表和数组 - 不变。为什么只是在一个方向插入?我期待(61,61)数组输出。 如果我觉得它会有帮助,我会包括实际的数字,但我不明白它会如何改变。我不明白griddata是如何工作的吗?

回答

0

下面是完整的代码:

import numpy as np 
from scipy.interpolate import griddata 

# random data to interpolate 
x = np.array([0, 10, 13, 17, 20, 50, 55, 60.0]) 
y = np.array([10, 20, 40, 80, 90, 95, 100, 120.0]) 
zg = np.random.randn(8, 8) 

#select one of the following two line, it depends on the order in z 
#xg, yg = np.broadcast_arrays(x[:, None], y[None, :]) 
xg, yg = np.broadcast_arrays(x[None, :], y[:, None]) 

yg2, xg2 = np.mgrid[y.min()-10:y.max()+10:100j, x.min()-10:x.max()+10:100j] 

zg2 = griddata((xg.ravel(), yg.ravel()), zg.ravel(), (xg2.ravel(), yg2.ravel()), method="nearest") 
zg2.shape = yg2.shape 

import pylab as pl 

pl.pcolormesh(xg2, yg2, zg2) 
pl.scatter(xg.ravel(), yg.ravel(), c=zg.ravel()) 

输出为:

enter image description here

+0

谢谢!! 似乎ravel()是答案,你的图形帮助我意识到我想要一个线性插值(现在可以与ravel一起使用),而不是最接近的插值! – adeedoubleu