我有一些data(x,y,z)位於非結構化網格上,我想插入數據用於可視化目的。2d非結構化網格數據的插值
我已經試過scipy.interpolate.griddata
,插值假設處處都是相同的值。之後,我嘗試了scipy.interpolate.Rbf
,但是這給我一個內存錯誤(請參閱下面的代碼)。
是否有其他方法或其他選項可以改善結果?
結果 - >
我的代碼
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import griddata, Rbf
x, y, z = np.loadtxt('stackoverflow_example-data')
# griddata
points = np.reshape(np.array([x, y]),(z.size, 2))
grid_x, grid_y = np.mgrid[x.min():x.max():1000j,y.min():y.max():1000j]
counts_I_grid_1 = griddata(points, z, (grid_x, grid_y), method='nearest')
counts_I_grid_2 = griddata(points, z, (grid_x, grid_y), method='linear', fill_value=0)
counts_I_grid_3 = griddata(points, z, (grid_x, grid_y), method='cubic', fill_value=0)
# Rbf -- fails due to memory error
#rbf = Rbf(x,y,z)
#counts_I_Rbf = rbf(grid_x,grid_y)
回溯(最近通話最後一個): 文件 「/path/code.py」 ,第14行 rbf = Rbf(x,y,z) 文件「/[...]/p (自我.xi,self.xi) 文件「/[...]/python3」 .4/site-packages/scipy/interpolate/rbf.py「,第222行,在_call_norm中 return self.norm(x1,x2) 文件」/[...]/python3.4/site-packages/scipy /interpolate/rbf.py」,線114,在_euclidean_norm 返回SQRT(((X1 - ×2)** 2)的.sum(軸= 0)) 的MemoryError
# plot the result
fig = plt.figure()
ax1 = plt.subplot(2,2,1)
plt.title('Data')
plt.gca().set_aspect((x.max() - x.min())/(y.max() - y.min()))
plt.scatter(x, y, c=z, s=2, edgecolor='', marker=',')
plt.colorbar(ax=ax1)
plt.xlim(x.min(), x.max())
plt.ylim(y.min(), y.max())
plt.xticks([20.7,20.9,21.1,21.3])
plt.ticklabel_format(useOffset=False)
ax2 = plt.subplot(2,2,2)
plt.title('nearest')
plt.imshow(counts_I_grid_1.T, origin='lower',
extent=(x.min(), x.max(), y.min(), y.max()),
aspect=(x.max() - x.min())/(y.max() - y.min()),
vmin=0,vmax=36)
plt.colorbar(ax=ax2)
plt.xticks([20.7,20.9,21.1,21.3])
plt.ticklabel_format(useOffset=False)
ax2 = plt.subplot(2,2,3)
plt.title('linear')
plt.imshow(counts_I_grid_2.T, origin='lower',
extent=(x.min(), x.max(), y.min(), y.max()),
aspect=(x.max() - x.min())/(y.max() - y.min()),
vmin=0,vmax=36)
plt.colorbar(ax=ax2)
plt.xticks([20.7,20.9,21.1,21.3])
plt.ticklabel_format(useOffset=False)
ax2 = plt.subplot(2,2,4)
plt.title('cubic')
plt.imshow(counts_I_grid_3.T, origin='lower',
extent=(x.min(), x.max(), y.min(), y.max()),
aspect=(x.max() - x.min())/(y.max() - y.min()),
vmin=0,vmax=36)
plt.colorbar(ax=ax2)
plt.xticks([20.7,20.9,21.1,21.3])
plt.ticklabel_format(useOffset=False)
plt.tight_layout()
plt.show()
1000倍1000 meshgrid?那是100萬分。有點多,你不覺得嗎?嘗試200次200. – MaxNoe