Numpy 将数据插值到一行点上

Numpy 将数据插值到一行点上,numpy,matplotlib,scipy,Numpy,Matplotlib,Scipy,我有一些间隔不规则的数据,需要对其进行分析。我可以使用mlab.griddata(或者更确切地说,它的natgrid实现)成功地将这些数据插入到常规网格中。这允许我使用pcolormesh和contour生成绘图、提取标高等。使用plot.contour,然后使用get_路径从contour CS.collections()中提取特定标高 现在,我想做的是,用我原来的不规则间隔的数据,在这个特定的等高线上插值一些量(也就是说,不是在一个规则的网格上)。Scipy中类似命名的griddata函数允

我有一些间隔不规则的数据,需要对其进行分析。我可以使用mlab.griddata(或者更确切地说,它的natgrid实现)成功地将这些数据插入到常规网格中。这允许我使用pcolormesh和contour生成绘图、提取标高等。使用plot.contour,然后使用get_路径从contour CS.collections()中提取特定标高

现在,我想做的是,用我原来的不规则间隔的数据,在这个特定的等高线上插值一些量(也就是说,不是在一个规则的网格上)。Scipy中类似命名的griddata函数允许这种行为,并且几乎可以正常工作。然而,我发现当我增加原始点的数量时,我可以在插值中得到奇怪的不稳定行为。我想知道是否有一种方法可以解决这个问题,也就是说,另一种方法可以将不规则间隔的数据(或者是规则间隔的数据,因为我可以使用mlab.griddata中的规则间隔的数据)插值到特定的行上

让我展示一些我所说的数字例子。看看这个数字:

左上角以点的形式显示我的数据,这条线显示了从这些点(x,y)的一些数据D中提取的级别=0。[注意,我有数据“D”、“能量”和“压力”,所有这些都在(x,y)空间中定义]。一旦我有了这条曲线,我就可以把D,能量和压力的插值量绘制到我的特定直线上。首先,注意D(中间,右侧)的绘图。所有点都应该是零,但不是所有点都是零。可能的原因是,对应于0标高的线是由来自mlab.griddata的一组统一点生成的,而“D”的绘图是由插入到该标高曲线上的原始数据生成的。你也可以在“能量”和“压力”中看到一些非物理的波动

好吧,看起来很简单,对吧?也许我应该沿着level=0曲线获得更多原始数据点。获取更多这些点,然后生成以下曲线图:

首先看左上角。你可以看到,我已经在level=0曲线附近的(x,y)空间中进行了采样。此外,您可以看到,我的新“D”图(中间,右侧)现在正确地插值到它原来没有插值到的区域中的零。但是现在我在曲线的开始处得到了一些摆动,在这个空间的“能量”和“压力”中也得到了一些其他的摆动!这对我来说还远远不是显而易见的,因为我的原始数据点仍然存在,我只是补充了一些额外的数据点。此外,我的插值变差的一些区域甚至不在我在第二次运行中添加的点附近——它们完全与我的原始点相邻

这就引出了我最初的问题。我担心生成“能量”、“D”和“压力”曲线的插值工作不正常(这是scigrid的griddata)。Mlab的griddata仅插值到常规网格,而我希望插值到左上角绘图中显示的这条特定线。我还有别的办法吗


谢谢你的时间

发布后,我决定尝试scipy.interpolate.SmoothBivariateSpline,结果如下:

你现在可以看到我的线是平滑的,所以看起来这会起作用。我会把这个标记为答案,除非有人很快发布了一些东西,暗示可能有更好的解决方案

编辑:根据要求,下面是用于生成这些图的一些代码。我没有一个简单的工作示例,上面的图是在一个更大的代码框架中生成的,但我将在下面用注释示意性地编写重要部分

# x,y,z are lists of data where the first point is x[0],y[0],z[0], and so on
minx=min(x)
maxx=max(x)
miny=min(y)
maxy=max(y)
# convert to numpy arrays
x=np.array(x)
y=np.array(y)
z=np.array(z)
# here we are creating a fine grid to interpolate the data onto
xi=np.linspace(minx,maxx,100)
yi=np.linspace(miny,maxy,100)
# here we interpolate our data from the original x,y,z unstructured grid to the new
# fine, regular grid in xi,yi, returning the values zi
zi=griddata(x,y,z,xi,yi)
# now let's do some plotting
plt.figure()
# returns the CS contour object, from which we'll be able to get the path for the 
# level=0 curve
CS=plt.contour(x,y,z,levels=[0]) 
# can plot the original data if we want
plt.scatter(x,y,alpha=0.5,marker='x')
# now let's get the level=0 curve
for c in CS.collections:
    data=c.get_paths()[0].vertices
# lineX,lineY are simply the x,y coordinates for our level=0 curve, expressed as arrays
lineX=data[:,0]
lineY=data[:,1]
# so it's easy to plot this too
plt.plot(lineX,lineY)
# now what to do if we want to interpolate some other data we have, say z2
# (also at our original x,y positions), onto 
# this level=0 curve?
# well, first I tried using scipy.interpolate.griddata == scigrid like so
origdata=np.transpose(np.vstack((x,y)))  # just organizing this data like the 
                                           # scigrid routine expects
lineZ2=scigrid(origdata,z2,data,method='linear')
# plotting the above curve (as plt.plot(lineZ2)) gave me really bad results, so 
# trying a spline approach
Z2spline=SmoothBivariateSpline(x,y,z2)  
# the above creates a spline object on our original data. notice we haven't EVALUATED
# it anywhere yet (we'll want to evaluate it on our level curve)
Z2Line=[]
# here we evaluate the spline along all our points on the level curve, and store the
# result as a new list 
for i in range(0,len(lineX)):
    Z2Line.append(Z2spline(lineX[i],lineY[i])[0][0])  # the [0][0] is just to get the 
                                                      # value, which is enclosed in 
                                                      # some array structure for some
                                                      # reason otherwise
# you can then easily plot this
plt.plot(Z2Line)

希望这对别人有帮助

你应该发布你使用的代码,以便人们可以使用它作为参考…@LuisMiguel,刚刚添加了一些代码。如果有什么不清楚的地方,请告诉我。@gammapoint谢谢!我在找类似的东西。获得分数。干杯