NumPy interp() function in Python also known as interpolation returns the one-dimensional piecewise linear interpolant to a function with given discrete data points (xp, fp). It is one-dimensional linear interpolation for monotonically increasing sample points.
In this article, I will explain how to use numpy.interp()
function and how to get the interpolated values of the NumPy array.
1. Quick Examples of Python NumPy Interpolate Function
If you are in a hurry, below are some quick examples of how to use the NumPy interpolate interp() function in Python.
# Below are the quick examples
# Example 1: Use numpy.interp() function
x = 5.8
xp = [3, 6, 8]
fp = [2, 5, 7]
arr2 = np.interp(x, xp, fp)
# Example 2: Get the interpolate values on graphical representation
arr = np.linspace(0, 2*np.pi, 12)
arr1 = np.sin(arr)
xvals = np.linspace(0, 2*np.pi, 60)
yinterp = np.interp(xvals, arr, arr1)
plt.plot(arr, arr1, 'o')
plt.plot(xvals, yinterp, '-x')
plt.show()
# Example 3: Get interpolation with periodic x-coordinates
x = [-120, -150, -135, 195, -30, -9, 5, 345]
xp = [180, -170, 250, -460]
fp = [8, 14, 5, 9]
arr2 = np.interp(x, xp, fp, period=360)
# Example 4: Use interp() function to numpy complex interpolation
x = [2.7, 6.0]
xp = [3, 5, 7]
fp = [2.0j, 0, 3+6j]
arr2 = np.interp(x, xp, fp)
2. Syntax of NumPy interp()
Following is the syntax of numpy.interp()
function.
# Syntax of numpy.interp()
numpy.interp(x, xp, fp, left=None, right=None, period=None)
2.1 Parameters of interp()
The numpy.interp()
function allows the following parameters
x
– array_like : The x-coordinates at which to evaluate the interpolated values.xp
– The x-coordinates of the data points, must be increasing if the argument period is not specified. Otherwise, xp is internally sorted after normalizing the periodic boundaries with xp = xp % period.fp
– The y-coordinates of the data points, same length as xp.left
– Optional float or complex corresponding to fp. left is the value to return for x < xp[0], by default optional values and fp[0].right
– Optional float or complex corresponding to fp. right is the value to return for x > xp[-1], by default optional values and fp[-1]period
– None or float, optional: period is the period for the x-coordinates. If it is given, left and right are ignored if period is specified. This is also optional.
2.2 Return Value of interp()
It returns the interpolated values of type float, the same shape as x. Raises ValueError if xp and fp have different length. If xp or fp are not 1-D sequences If period == 0.
3. Use numpy.interp() Function
Let’s use the NumPy interp() (interpolation) of the array by using x and y coordinates.
import numpy as np
x = 5.8
xp = [3, 6, 8]
fp = [2, 5, 7]
# Use numpy.interp() function
arr2 = np.interp(x, xp, fp)
print (arr2)
# Output
# 4.8
4. Get the Interpolate Values on Graphical Representation
We can also use the NumPy interp() function for graphical representation using the MatLab library. Plot an interpolant to the sine function.
import numpy as np
import matplotlib.pyplot as plt
# Get the interpolate values on graphical representation
arr = np.linspace(0, 2*np.pi, 12)
arr1 = np.sin(arr)
xvals = np.linspace(0, 2*np.pi, 60)
yinterp = np.interp(xvals, arr, arr1)
plt.plot(arr, arr1, 'o')
plt.plot(xvals, yinterp, '-x')
plt.show()
Yields below output.

You can see the Parabolic graph of the interp()
method in Numpy.
5. Get Interpolation with Periodic x-coordinates
import numpy as np
x = [-120, -150, -135, 195, -30, -9, 5, 345]
xp = [180, -170, 250, -460]
fp = [8, 14, 5, 9]
# Get interpolation with periodic x-coordinates
arr2 = np.interp(x, xp, fp, period=360)
print(arr2)
6. Use interp() Function to NumPy Complex Interpolation
We can also get the Interpolate value of complex elements of an array. For example, before snippet uses the complex type.
import numpy as np
x = [2.7, 6.0]
xp = [3, 5, 7]
fp = [2.0j, 0, 3+6j]
# Use interp() function to numpy complex interpolation
arr2 = np.interp(x, xp, fp)
print(arr2)
# Output
# [0. +2.j 1.5+3.j]
Conclusion
In this article, I have explained how to use numpy.interp()
function to get the interpolated values, same shape as x with examples.
Happy Learning!!
Related Articles
- How to Transpose Matrix in NumPy
- NumPy where() Multiple Conditions
- How To Use NumPy dot() Function in Python
- Numpy loadtxt() Explained with Examples
- How to use Python NumPy arange() Function
- Python NumPy Split Array – Using split() Function
- How to Calculate minimum() of Array in NumPy?
- How to Calculate Maximum of Array in NumPy
- NumPy power() Function in Python