Posted : admin On 1/26/2022
2011-11-21 16:29:36 UTC
I'm converting a MatLab program to Python, and I'm having problems understanding why scipy.interpolate.interp1d is giving different results than MatLab interp1.
In MatLab the usage is slightly different:
yi = interp1(x,Y,xi,'cubic')
While in SciPy it's like this:
f = interp1d(x,Y,kind='cubic')
yi = f(xi)
For a trivial example the results are the same:
interp1([0 1 2 3 4], [0 1 2 3 4],[1.5 2.5 3.5],'cubic')
1.5000 2.5000 3.5000
array([ 1.5, 2.5, 3.5])
But for a real-world example they are not the same:
x = 0.0000e+000 2.1333e+001 3.2000e+001 1.6000e+004 2.1333e+004 2.3994e+004
Y = -6 -6 20 20 -6 -6
xi = 0.00000 11.72161 23.44322 35.16484
Matlab: -6.0000 -12.3303 -3.7384 22.7127
Python: -6. -15.63041012 -2.04908267 30.43054192
Any thoughts as to how I can get results that are consistent with MatLab?
Series.interpolate(self, method='linear', axis=0, limit=None, inplace=False, limit_direction='forward', limit_area=None, downcast=None, **kwargs)[source]

Octave-Forge is a collection of packages providing extra functionality for GNU Octave. Here are the examples of the python api scipy.interpolate.interp1d taken from open source projects. By voting up you can indicate which examples are most useful and appropriate. 200 Examples prev 1 2 3 4.

Interpolate values according to different methods.


Please note that only method='linear' is supported forDataFrame/Series with a MultiIndex.

method:str, default ‘linear’

Interpolation technique to use. One of:

  • ‘linear’: Ignore the index and treat the values as equallyspaced. This is the only method supported on MultiIndexes.
  • ‘time’: Works on daily and higher resolution data to interpolategiven length of interval.
  • ‘index’, ‘values’: use the actual numerical values of the index.
  • ‘pad’: Fill in NaNs using existing values.
  • ‘nearest’, ‘zero’, ‘slinear’, ‘quadratic’, ‘cubic’, ‘spline’,‘barycentric’, ‘polynomial’: Passed toscipy.interpolate.interp1d. These methods use the numericalvalues of the index. Both ‘polynomial’ and ‘spline’ require thatyou also specify an order (int), e.g.df.interpolate(method='polynomial',order=5).
  • ‘krogh’, ‘piecewise_polynomial’, ‘spline’, ‘pchip’, ‘akima’:Wrappers around the SciPy interpolation methods of similarnames. See Notes.
  • ‘from_derivatives’: Refers toscipy.interpolate.BPoly.from_derivatives whichreplaces ‘piecewise_polynomial’ interpolation method inscipy 0.18.

New in version 0.18.1: Added support for the ‘akima’ method.Added interpolate method ‘from_derivatives’ which replaces‘piecewise_polynomial’ in SciPy 0.18; backwards-compatible withSciPy < 0.18

axis:{0 or ‘index’, 1 or ‘columns’, None}, default None

Axis to interpolate along.

limit:int, optional

Maximum number of consecutive NaNs to fill. Must be greater than0.

inplace:bool, default False

Update the data in place if possible.

limit_direction:{‘forward’, ‘backward’, ‘both’}, default ‘forward’

If limit is specified, consecutive NaNs will be filled in thisdirection.

limit_area:{None, ‘inside’, ‘outside’}, default None

If limit is specified, consecutive NaNs will be filled with thisrestriction.

  • None: No fill restriction.
  • ‘inside’: Only fill NaNs surrounded by valid values(interpolate).
  • ‘outside’: Only fill NaNs outside valid values (extrapolate).
downcast:optional, ‘infer’ or None, defaults to None

Downcast dtypes if possible.


Keyword arguments to pass on to the interpolating function.

Series or DataFrame

Returns the same object type as the caller, interpolated atsome or all NaN values.

The SciPy documentation explains that interp1d 's kind argument can take the values ‘linear’, ‘nearest’, ‘zero’, ‘slinear’, ‘quadratic’, ‘cubic’. The last three are spline orders and 'linear' is self-explanatory. What do 'nearest' and 'zero' do? Apr 14, 2020 For most of the interpolation methods scipy.interpolate.interp1d is used in the background. This class returns a function whose call method uses interpolation to find the value of new points. Here are some of the interpolation methods which uses scipy backend nearest, zero, slinear, quadratic, cubic, spline, barycentric, polynomial. Hi All I am a very recent member of this list so apologise if this matter has been raised previously I have an Excel workbook that uses numerous instances of Interp1d from XonGrid 64. I give this workbook free to students and colleagues with the instruction to install the add-In.


See also

Fill missing values using different methods.
Piecewise cubic polynomials (Akima interpolator).
Piecewise polynomial in the Bernstein basis.
Interpolate a 1-D function.
Interpolate polynomial (Krogh interpolator).
PCHIP 1-d monotonic cubic interpolation.
Cubic spline data interpolator.


The ‘krogh’, ‘piecewise_polynomial’, ‘spline’, ‘pchip’ and ‘akima’methods are wrappers around the respective SciPy implementations ofsimilar names. These use the actual numerical values of the index.For more information on their behavior, see theSciPy documentationand SciPy tutorial.


Filling in NaN in a Series via linearinterpolation.

Filling in NaN in a Series by padding, but filling at most twoconsecutive NaN at a time.

Filling in NaN in a Series via polynomial interpolation or splines:Both ‘polynomial’ and ‘spline’ methods require that you also specifyan order (int).

Interp1d Numpy

Fill the DataFrame forward (that is, going down) along each columnusing linear interpolation.

Note how the last entry in column ‘a’ is interpolated differently,because there is no entry after it to use for interpolation.Note how the first entry in column ‘b’ remains NaN, because thereis no entry befofe it to use for interpolation.


Interp1d Extrapolate

Using polynomial interpolation.