def spline_points(cls, X, Y, Z, xi, yi): """Interpolates for given set of points""" # SBS requires at least m=(kx+1)*(ky+1) points, # where kx=ky=3 (default) is the degree of bivariate spline. # Thus, if less than 16=(3+1)*(3+1) points, adjust kx & ky. spline = SBS(X, Y, Z) return spline(xi, yi)
#!/usr/bin/env python3 #-*- coding:utf-8 -*- __author__ = 'tongzi' import numpy as np from scipy.interpolate import SmoothBivariateSpline as SBS import matplotlib.pyplot as plt #定义一个波纹函数 ripple = lambda x, y: np.sqrt(x**2 + y**2) + np.sin(x**2 + y**2) #采样数据,用于插值 xy = np.random.rand(1000, 2) x, y = xy[:, 0], xy[:, 1] sample = ripple(xy[:, 0] * 5, xy[:, 1] * 5) #插值 fit = SBS(x * 5, y * 5, sample, s=0.01, kx=4, ky=4) interp = fit(np.linspace(0, 5, 1000), np.linspace(0, 5, 1000)) plt.imshow(interp) plt.show()
import matplotlib.pyplot as plt from scipy.interpolate import SmoothBivariateSpline as SBS # Defining a function ripple = lambda x, y: np.sqrt(x**2 + y**2) + np.sin(x**2 + y**2) # Generarting gridded data grid_x, grid_y = np.mgrid[0:5:1000j, 0:5:1000j] # Generating saplte that interpolation function will see xy = np.random.rand(1000, 2) x, y = xy[:, 0], xy[:, 1] saplte = ripple(xy[:, 0] * 5, xy[:, 1] * 5) # Interpolating data fit = SBS(x * 5, y * 5, saplte, s=0, kx=4, ky=4) interp = fit(np.linspace(0, 5, 1000), np.linspace(0, 5, 1000)) # Making figure. fig = plt.figure(figsize=(8, 4)) x0, x1 = 0, 1000 y0, y1 = 0, 1000 ax1 = fig.add_subplot(121) ax1.imshow(ripple(grid_x, grid_y).T, cmap=plt.cm.PRGn, interpolation='nearest') ax1.scatter(xy[:, 0] * 1e3, xy[:, 1] * 1e3, facecolor='black', edgecolor='none', s=1) ax1.xaxis.set_visible(False)
# Interpolate onto our grid print( ("Starting ST4 interpolation for init time {0}".format(date))) if method1: ST4data_SALgrid = griddata( (ST4_SALgrid_xx.flat, ST4_SALgrid_yy.flat), ST4data_ST4grid.flat, (SAL_native_mx.flat, SAL_native_my.flat), method='nearest').reshape(308, 537) elif method2: ST4rbs = RBS(ST4_SALgrid_xx, ST4_SALgrid_yy, ST4data_ST4grid) ST4data_SALgrid = N.swapaxes( ST4rbs(SAL_native_xx, SAL_native_yy), 1, 0) elif method3: ST4_F = SBS(ST4_SALgrid_xx.flatten(), ST4_SALgrid_yy.flatten(), ST4data_ST4grid.flatten()) ST4data_SALgrid = ST4_F(SAL_native_xx, SAL_native_yy) print("Done.") # import pdb; pdb.set_trace() obplotproj = False # TEST if obplotproj: SALmap.drawcoastlines() SALmap.drawstates() data2 = data_SALgrid.reshape(308, 537) SALmap.contourf(SAL_native_mx, SAL_native_my, data2, levels=N.arange(5, 75, 5)) plt.savefig(