2014-11-14 1 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

+0

코드로 무엇을하려고하는지 잘 모르겠지만,'np.meshgrid'의 Matlabesque 동작에 빠져들 수도 있습니다. 이 토론 [여기] (https://github.com/scipy/scipy/issues/3164)을 참조하십시오. TLDR : meshgrid의 입력 순서를 변경하고 다시 시도하십시오. – cd98

+0

그래, 그냥 변경하십시오 :'grid_y, grid_x = np.meshgrid (xi, yi)'그리고 나는 지금 일해야한다고 생각합니다. (이것이 문제라는 것을 확인했다면 답안에서 일어난 일을 설명해주세요.이 질문으로 돌아 오는 사람들이 정확히 무슨 일이 일어 났는지 알 수 있습니다.) – cd98

답변

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() 
관련 문제