0

I am a bit stumped on this one. I have used spline to smooth my data successfully, however it is just not working this time. Here is the snippet of the code that is not working. Any pointers would be highly appreciated.

In [46]: x

Out[46]:
array([  600.,   650.,   700.,   750.,   800.,   850.,   900.,   950.,
        1000.,  1050.,  1100.,  1150.,  1200.,  1250.])

In [47]: y

Out[47]:
array([ 2.68530481,  3.715443  ,  4.11270841,  2.91720571,  1.49194971,
        0.24770035, -0.64713611, -1.40938122, -2.24634466, -3.04577225,
       -3.73914759, -4.35097303, -4.94702689, -5.56523414])

In [48]: x2=numpy.linspace(x.min(),x.max(),20)

In [49]: spline(x,y,x2)

Out[49]:
array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.,  0.,  0.,  0.])
A.V.
  • 1
  • 1
  • 1
  • Check if this SO question is relevant: [smooth line with spline and datetime objects doesn't work](http://stackoverflow.com/questions/38686415/smooth-line-with-spline-and-datetime-objects-doesnt-work/38687139#38687139) – Mark Mikofski Nov 02 '16 at 05:16

1 Answers1

0

enter image description here

Try using interp1d instead of spline which is deprecated(*):

import numpy as np
from matplotlib import pyplot as plt
from scipy.interpolate import interp1d

plt.ion()
x = np.array([600., 650., 700., 750., 800., 850., 900., 950.,
              1000., 1050., 1100., 1150., 1200., 1250.])
y = np.array([2.68530481, 3.715443, 4.11270841, 2.91720571, 1.49194971,
              0.24770035, -0.64713611, -1.40938122, -2.24634466,
              -3.04577225, -3.73914759, -4.35097303, -4.94702689,
              -5.56523414])
plt.plot(x,y)
x2 = np.linspace(x.min(), x.max(), 20)
f = interp1d(x, y, kind='cubic')
y2 = f(x2)
plt.plot(x2,y2)

Output

In [20]: x2
Out[20]:
array([  600.        ,   634.21052632,   668.42105263,   702.63157895,
         736.84210526,   771.05263158,   805.26315789,   839.47368421,
         873.68421053,   907.89473684,   942.10526316,   976.31578947,
        1010.52631579,  1044.73684211,  1078.94736842,  1113.15789474,
        1147.36842105,  1181.57894737,  1215.78947368,  1250.        ])

In [21]: y2
Out[21]:
array([ 2.68530481,  3.35699957,  4.03277746,  4.08420565,  3.31233485,
        2.29896296,  1.34965136,  0.48288214, -0.21322503, -0.76839036,
       -1.28566315, -1.84433723, -2.42194321, -2.96633554, -3.45993064,
       -3.90553288, -4.31968149, -4.7262301 , -5.13883472, -5.56523414])

(*) Under additional tools, scipy lists spline as:

Functions existing for backward compatibility (should not be used in new code):

Mark Mikofski
  • 19,398
  • 2
  • 57
  • 90
  • Thanks a lot. I also figured out that I got my x and y as a cut from a 2d array and the shape might be playing a trick on me. When I changed x=numpy.array(x) and y=numpy.array(y), it worked. But I shall move to interp1d. Thanks for pointing that out. – A.V. Nov 02 '16 at 05:44
  • @A.V. did my answer help you at all? Not to be petty, but if it did, can you please select it as the correct answer. Otherwise, never mind. Glad to be of service. – Mark Mikofski Nov 02 '16 at 16:14