Question

I'd like to generate a smoothed spline using a common set of x-values and three sets of y-values:

xs = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

ys1 = [0.2, 0.3, 0.1, 0.8, 0.7, 0.2, 0.1, 0.8, 0.9, 1.1]
ys2 = [0.1, 0.2, 0.4, 0.5, 0.8, 0.3, 0.2, 0.9, 1.2, 1.2]
ys3 = [0.1, 0.1, 0.4, 0.1, 0.9, 0.3, 0.1, 0.8, 0.9, 0.9]

Looking at the spline documentation, I'm not sure if I am using the correct functions, but I'm trying to create my spline using RectBivariateSpline. What I'm trying to do is something like this, but I'm getting a number of errors, which makes me think I may be using the wrong function?

import scipy as scip
from scipy.interpolate import RectBivariateSpline

x_arr = scip.array(x_arr)
y_arr = scip.vstack((ys1, ys2, ys3))
spline = RectBivariate(x_arr, x_arr, y_arr)
Was it helpful?

Solution

The RectBivariateSpline is used with for a 3D-spline where the Z coordinate is given as a 2D array, with each element [i,j] corresponding to the positions x[i] and y[j].

In your case it looks more that you have 3 X 2D-splines, which you can compute using:

import scipy
from scipy.interpolate import spline
xnew = scipy.linspace(0, 10, 1000)
ys1 = spline(xs, ys1, xnew, order=3)
ys2 = spline(xs, ys2, xnew, order=3)
ys3 = spline(xs, ys3, xnew, order=3)

If you want to take an average spline (like clarified in your comments):

ys = np.vstack((ys1,ys2,ys3)).mean(axis=0)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top