Better Programming

Advice for programmers.

Member-only story

Python Spline Interpolation How-To

Lev Maximov
Better Programming
Published in
6 min readAug 15, 2022

--

Image Credit: own work

If you ever interpolated a function in Python, you probably wondered why there are so many ways to do one simple thing. 2D interpolation methods are especially mind-blowing since they use incompatible conventions about x- and y-axis order. Here’s a brief summary of when to use which and what argument follows which convention.

One Dimension

The 1D case is relatively simple. Each interpolation function has its own distinct set of features:

UnivariateSpline is the only one that can do both interpolation and fitting. It also accepts custom weights for the nodes.

interp1d borrows its name convention from MATLAB. For the default linear case, it uses Python (NumPy if possible) for quadratic and above—Fortran.

CubicSpline, as opposed to other methods that are thin Fortran wrappers, is implemented mostly in Python (=less cryptic error messages; might be a tad slower) and only uses Fortran for sparse matrix heavy lifting. Only CubicSpline provides a way to choose between various boundary conditions…

--

--