Interpolation (np.interp)

Reviewed & published by Brayan K

Interpolation estimates the value of a curve between the data points you actually measured, and np.interp does it with fast straight-line (linear) interpolation in a single call.

Part of the free NumPy course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

You'll read values between known samples, resample a whole curve onto a finer grid, control what happens past the edges with left and right, and learn why xp must be sorted.

What You'll Learn in This Lesson

1 Reading Between the Points

np.interp(x, xp, fp) takes three things: the query x where you want a value, the known x-coordinates xp, and the matching y-values fp. It finds the two surrounding known points and returns the value on the straight line between them. The query can be a single number or an array.

import numpy as np

xp = np.array([0, 1, 2, 3])     # known x
fp = np.array([0, 10, 20, 30])  # known y

print(np.interp(0.5, xp, fp))   # 5.0
print(np.interp(1.5, xp, fp))   # 15.0

# Pass an array of query points
print(np.interp([0.5, 2.5], xp, fp))  # [ 5. 25.]

# ✅ Expected output:
# 5.0
# 15.0
# [ 5. 25.]

2 Resampling a Curve

A common use is resampling: you have a curve measured at a few points and you want it on a denser, evenly spaced grid (to plot smoothly or to line two datasets up). Build the new grid with np.linspace, then interpolate the original onto it.

import numpy as np

xp = np.array([0, 2, 4, 6, 8])    # measured at every 2 units
fp = np.array([0, 4, 8, 6, 0])

xnew = np.linspace(0, 8, 9)       # one point per unit
print(xnew)
# [0. 1. 2. 3. 4. 5. 6. 7. 8.]

print(np.interp(xnew, xp, fp))
# [0. 2. 4. 6. 8. 7. 6. 3. 0.]

# ✅ Expected output:
# [0. 1. 2. 3. 4. 5. 6. 7. 8.]
# [0. 2. 4. 6. 8. 7. 6. 3. 0.]

3 Edges: left, right, and Sorted xp

Outside the known range, np.interp does not extrapolate — it clamps to the first and last fp values. Override that with left (for x below xp[0]) and right (for x above xp[-1]). And remember: xp must be sorted ascending, or the results are silently wrong.

import numpy as np

xp = np.array([1, 2, 3])
fp = np.array([10, 20, 30])

# Default: clamp to the nearest end value
print(np.interp(0, xp, fp))   # 10.0  (below range)
print(np.interp(5, xp, fp))   # 30.0  (above range)

# Override the out-of-range behaviour
print(np.interp(0, xp, fp, left=-1))   # -1.0
print(np.interp(5, xp, fp, right=99))  # 99.0
print(np.interp([0, 5], xp, fp, left=-1, right=99))  # [-1. 99.]

# ✅ Expected output:
# 10.0
# 30.0
# -1.0
# 99.0
# [-1. 99.]

If your data is not already sorted by x, sort it first — e.g. order = np.argsort(xp) then index both arrays with order — before calling np.interp.

🎯 YOUR TURN: Fill in the Blanks

Replace each ___ so the program reads two interpolated values off a simple line.

import numpy as np

xp = np.array([0, 10, 20])     # known x
fp = np.array([0, 100, 200])   # known y

# Estimate the value at x = 5 and x = 15
print(np.___(5, xp, fp))    # fill in: interp -> 50.0
print(np.interp(___, xp, fp))  # fill in: 15   -> 150.0

# Expected output:
# 50.0
# 150.0

Expected output: 50.0 then 150.0. (Answers: interp, 15.)

⚠️ Common Errors & Quick Reference

❌ Unsorted xp gives wrong answers

np.interp assumes xp increases and never sorts it for you.

✅ Fix: sort first — order = np.argsort(xp), then np.interp(x, xp[order], fp[order]).

❌ Expecting extrapolation past the edges

Outside the range it clamps to the end values instead of continuing the trend.

✅ Fix: use left/right for custom edges, or a dedicated tool like SciPy for true extrapolation.

TaskCode
Value at one pointnp.interp(1.5, xp, fp)
Many points at oncenp.interp(xnew, xp, fp)
Custom out-of-rangenp.interp(x, xp, fp, left=0, right=0)
Sort before interpolatingo = np.argsort(xp)

🎯 Mini Challenge: Hourly Temperatures

Temperatures were logged every 6 hours. Estimate the temperature at 3, 9, 15, and 21 hours by interpolating between the logged values.

import numpy as np

hours = np.array([0, 6, 12, 18, 24])
temp  = np.array([10, 14, 22, 18, 11])

query = np.array([3, 9, 15, 21])
print(np.interp(query, hours, temp))
# [12.  18.  20.  14.5]

# ✅ Expected output:
# [12.  18.  20.  14.5]

❓ Frequently Asked Questions

Lesson complete — interpolation unlocked!

You can now estimate values between known points with np.interp, resample a curve onto a finer grid, control the edges with left and right, and keep xp sorted for correct results.

Practice quiz

What kind of interpolation does np.interp perform?

  • One-dimensional linear (straight-line) interpolation
  • Cubic spline interpolation
  • Nearest-neighbour only
  • Polynomial fitting

Answer: One-dimensional linear (straight-line) interpolation. np.interp draws straight lines between known points for 1D linear interpolation.

What are the three main arguments of np.interp(x, xp, fp)?

  • start, stop, step
  • rows, cols, depth
  • query x, known x-coords xp, known y-values fp
  • mean, std, count

Answer: query x, known x-coords xp, known y-values fp. x is where you want a value, xp are the known x's, fp the matching y's.

For xp=[0,1,2,3], fp=[0,10,20,30], what is np.interp(1.5, xp, fp)?

  • 10.0
  • 20.0
  • 15.0
  • 1.5

Answer: 15.0. 1.5 is halfway between (1,10) and (2,20), giving 15.0.

By default, what does np.interp do for x above xp[-1]?

  • Extrapolates the trend
  • Raises an error
  • Returns NaN
  • Clamps to the last fp value

Answer: Clamps to the last fp value. It clamps: any x above the range returns fp[-1] rather than extrapolating.

What requirement must xp satisfy for correct results?

  • It must be sorted ascending (monotonic)
  • It must be all integers
  • It must be the same as fp
  • It must contain zero

Answer: It must be sorted ascending (monotonic). np.interp assumes xp increases; unsorted xp gives silently wrong answers.

Which keyword sets the value returned for x below xp[0]?

  • low
  • left
  • below
  • start

Answer: left. left= overrides the clamped value for x below the first known x.

For xp=[1,2,3], fp=[10,20,30], what is np.interp(0, xp, fp, left=-1)?

  • 10.0
  • 0.0
  • -1.0
  • 20.0

Answer: -1.0. x=0 is below the range, so left=-1 is returned instead of clamping to 10.

If your data is not sorted by x, what should you do first?

  • Reverse fp
  • Use np.interp twice
  • Nothing, interp sorts it
  • Sort both arrays, e.g. with np.argsort(xp)

Answer: Sort both arrays, e.g. with np.argsort(xp). Sort xp and fp together (an argsort on xp) before calling np.interp.

Can np.interp accept an array of query points at once?

  • Yes, x can be a scalar or an array
  • No, only one value at a time
  • Only if xp is a scalar
  • Only for integer queries

Answer: Yes, x can be a scalar or an array. The query x may be a single number or a whole array of points.

For xp=[0,10,20], fp=[0,100,200], what is np.interp(5, xp, fp)?

  • 50.0
  • 5.0
  • 100.0
  • 10.0

Answer: 50.0. 5 is halfway between (0,0) and (10,100), giving 50.0.

Continue this course

Frequently asked questions

What does np.interp do?

np.interp(x, xp, fp) performs one-dimensional linear interpolation. Given known data points defined by the x-coordinates xp and matching y-values fp, it estimates the y-value at any new x by drawing straight lines between the nearest known points. It is the quickest way to read a value off a curve between samples, resample a series onto a new grid, or fill in intermediate points.

Does xp need to be sorted for np.interp?

Yes. np.interp assumes xp is monotonically increasing and does not sort it for you. If xp is unsorted the results are silently wrong, so sort both arrays together first, for example with an argsort on xp, before calling np.interp. The query points x can be in any order and can be a scalar or an array.

What happens outside the range of xp?

By default np.interp clamps: any x below xp[0] returns fp[0] and any x above xp[-1] returns fp[-1], so it never extrapolates. You can override these edge values with the left and right keyword arguments, for example np.interp(x, xp, fp, left=0, right=0) to return 0 outside the known range instead of clamping.