2014-11-14 53 views
0

我想在給定的點上繪製曲面。我想獲得任意點的z座標。我不明白爲什麼scipy.interpolate.griddata和scipy.interpolate.RectBivariateSpline爲相同的x和y座標返回不同的值。我哪裏做錯了?爲什麼scipy.interpolate.griddata和scipy.interpolate.RectBivariateSpline返回不同​​的值?

import numpy as np 
from scipy.interpolate import griddata, RectBivariateSpline 
from mpl_toolkits.mplot3d import Axes3D 
import matplotlib.pyplot as plt 


x_list = np.array([ 10.0, 10.0, 0.0, 0.0]) 
y_list = np.array([ 0.0, 10.0, 10.0, 0.0]) 
z_list = np.array([103.95, 105.5, 104.85, 104.6]) 

xi = np.linspace(min(x_list), max(x_list),11) 
yi = np.linspace(min(y_list), max(y_list),11) 

grid_x, grid_y = np.meshgrid(xi, yi) 

grid_z1 = griddata((x_list, y_list), z_list, (grid_x, grid_y), method='nearest') 
grid_z2 = griddata((x_list, y_list), z_list, (grid_x, grid_y), method='linear') 
grid_z3 = griddata((x_list, y_list), z_list, (grid_x, grid_y), method='cubic') 

z = RectBivariateSpline(xi, yi, grid_z2, kx=1, ky=1, s=0) 

print z(10.0, 0.0)[0,0] #return 104.85!!! Must be 103.95. 

fig = plt.figure() 
ax1 = fig.add_subplot(221, projection='3d') 
surf = ax1.plot_surface(grid_x, grid_y, grid_z1) 
ax1.set_xlabel(u'X') 
ax1.set_ylabel(u'Y') 
ax1.set_zlabel(u'Z') 

ax2 = fig.add_subplot(222, projection='3d') 
surf = ax2.plot_surface(grid_x, grid_y, grid_z2) 
ax2.set_xlabel(u'X') 
ax2.set_ylabel(u'Y') 
ax2.set_zlabel(u'Z') 

ax3 = fig.add_subplot(223, projection='3d') 
surf = ax3.plot_surface(grid_x, grid_y, grid_z3) 
ax3.set_xlabel(u'X') 
ax3.set_ylabel(u'Y') 
ax3.set_zlabel(u'Z') 

plt.show() 
+0

它們不就是不同的插值方法嗎?你不能指望這些值完全相同。 – BrenBarn 2014-11-14 22:34:16

+0

我不確定你想要用你的代碼做什麼,但是你可能會陷入'np.meshgrid'的Matlabesque行爲。看到這個討論[這裏](https://github.com/scipy/scipy/issues/3164)。 TLDR:改變meshgrid的輸入順序,然後再試一次 – cd98 2014-11-14 22:59:15

+0

是的,只需要改變:'grid_y,grid_x = np.meshgrid(xi,yi)',我認爲現在應該起作用。 (如果您確認這是問題,請解釋回答中發生了什麼,以便回到此問題的人確切知道發生了什麼) – cd98 2014-11-14 23:02:49

回答

1

我使用參數indexing = 'ij'和我的代碼正常工作。

import numpy as np 
from scipy.interpolate import griddata, RectBivariateSpline 
from mpl_toolkits.mplot3d import Axes3D 
import matplotlib.pyplot as plt 

x_list = np.array([ 10.0, 10.0, 0.0, 0.0]) 
y_list = np.array([ 0.0, 10.0, 10.0, 0.0]) 
z_list = np.array([103.95, 105.5, 104.85, 104.6]) 

xi = np.linspace(min(x_list), max(x_list),11) 
yi = np.linspace(min(y_list), max(y_list),11) 

grid_x, grid_y = np.meshgrid(xi, yi, indexing = 'ij') 

grid_z1 = griddata((x_list, y_list), z_list, (grid_x, grid_y), method='nearest') 
grid_z2 = griddata((x_list, y_list), z_list, (grid_x, grid_y), method='linear') 
grid_z3 = griddata((x_list, y_list), z_list, (grid_x, grid_y), method='cubic') 

z = RectBivariateSpline(xi, yi, grid_z2, kx=1, ky=1, s=0) 

print z(10.0, 0.0)[0,0] #return 103.95))) 

fig = plt.figure() 
ax1 = fig.add_subplot(221, projection='3d') 
surf = ax1.plot_surface(grid_x, grid_y, grid_z1) 
ax1.set_xlabel(u'X') 
ax1.set_ylabel(u'Y') 
ax1.set_zlabel(u'Z') 

ax2 = fig.add_subplot(222, projection='3d') 
surf = ax2.plot_surface(grid_x, grid_y, grid_z2) 
ax2.set_xlabel(u'X') 
ax2.set_ylabel(u'Y') 
ax2.set_zlabel(u'Z') 

ax3 = fig.add_subplot(223, projection='3d') 
surf = ax3.plot_surface(grid_x, grid_y, grid_z3) 
ax3.set_xlabel(u'X') 
ax3.set_ylabel(u'Y') 
ax3.set_zlabel(u'Z') 

plt.show() 
相關問題