1

我目前有一个大约 100x120 2d numpy 矩阵。索引指的是“坐标”,矩阵中的值是“高度”,我正在尝试使用类似于此处显示的“二维样条表示”的 pcolor 绘制此数据:

http://docs.scipy.org/doc/scipy-0.7.x/reference/tutorial/interpolate.html

我的问题是,虽然我所有的“坐标”和“高度”都是整数,但坐标不是均匀的空间。例如,只有特定的行包含数据(不等间距),并且包含数据的每一行对于其他每个条目都只有一个“高度”值(每行包含数据的间距相同)。我的意思的一个简单例子如下:

[[nan,   3, nan,   1, nan,   2], 
 [nan, nan, nan, nan, nan, nan],
 [nan,   5, nan,   2, nan,   3],
 [nan, nan, nan, nan, nan, nan],
 [nan, nan, nan, nan, nan, nan],
 [nan, nan, nan, nan, nan, nan],
 [nan,   4, nan,   1, nan,   2]]

我一直在尝试遵循我链接到的插值/pcolor 示例,但没有成功。我的目标是在我的所有数据上绘制一个漂亮的连续 pcolor 类型图,在带有数据的点之间进行插值以填充这些 nan。

如果您能提供任何帮助,我将不胜感激。

4

1 回答 1

4

What's the problem? You just need to extract the indexes of the cells with values and pass those to the interpolate function with the 'height' values. There's some code that does this below.

import numpy as np
from numpy import nan
from scipy import interpolate
import matplotlib.pyplot as plt

a = np.array([[nan,   3, nan,   1, nan,   2], 
 [nan, nan, nan, nan, nan, nan],
 [nan,   5, nan,   2, nan,   3],
 [nan, nan, nan, nan, nan, nan],
 [nan, nan, nan, nan, nan, nan],
 [nan, nan, nan, nan, nan, nan],
 [nan,   4, nan,   1, nan,   2]])

x, y = np.where(np.isfinite(a))
z = a[x,y]

xnew,ynew = np.mgrid[0:6:70j,0:6:70j]
tck = interpolate.bisplrep(x,y,z,s=0, kx=1, ky=1)
znew = interpolate.bisplev(xnew[:,0],ynew[0,:],tck)

plt.figure()
plt.pcolor(xnew,ynew,znew)
plt.colorbar()
plt.title("Interpolated function.")
plt.show()

The result will look something this:

enter image description here

Note that this doesn't match the exact orientation of your matrix. To do so you would have to change the origin of the plot to be in the top left corner and possibly transpose the data. I'll leave that as an exercise to you.

Also, the method of getting the indexes of the non-na values is a little crude, so perhaps someone else could comment on that (thanks to seberg for the tip).

于 2012-10-23T15:21:21.260 回答