Mike C. Mike C. - 1 month ago 16
Python Question

Scipy Spline producing an array full of zeros

I am attempting to smooth a curve using spline in the Scipy module. I am using Python 2.7 on a 64 bit Windows 10 machine. I am pretty sure I have the code written correctly but I get a numpy array full of zeros for my y axis. Here's the code:

#Smooth Chart Curves
def interp(self, x, y):
xx = np.array(x)
yy = np.array(y)
xnew = np.linspace(xx.min(), xx.max(), 300)
ynew = spline(xx, yy, xnew)
xnew = np.array(xnew).tolist()
ynew = np.array(ynew).tolist()
return xnew, ynew


I begin by passing a list of six numbers to both x and y variables. The
xnew
variable works fine, but
ynew = spline(xx, yy, xnew)
creates a numpy array that is filled with 300 zeros.

I am passing the following as x and y:

x = [2800.0, 2002.0, 1234.0, 800.0, 450.0, 0]
y = [5.0, 9.1, 15.3, 20.9, 25.4, 27.2]

Answer

Your x axis needs to be monotonically increasing for interpolation to work. Maybe it is a typo, but y looks much more like the x axis to me.

If it really isn't:

xx = np.array(x)
sx = np.argsort(xx)
xx = xx[sx]
yy = np.array(y)
yy = yy[sx]

will sort both x and y so that x is sorted in ascending order.

Comments