0
0
SciPydata~10 mins

2D interpolation (interp2d, griddata) in SciPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - 2D interpolation (interp2d, griddata)
Start with scattered 2D points
Choose interpolation method
interp2d: grid-based
Create interpolation function
Input new points to estimate values
Output interpolated values on new points
We start with scattered 2D data points, choose an interpolation method (interp2d or griddata), create an interpolation function, then input new points to get estimated values.
Execution Sample
SciPy
import numpy as np
from scipy.interpolate import interp2d, griddata

x = np.array([0, 1, 2])
y = np.array([0, 1, 2])
z = np.array([[0, 1, 4], [1, 2, 5], [4, 5, 8]])

f_interp2d = interp2d(x, y, z, kind='linear')
zi = f_interp2d(1.5, 1.5)

points = np.array([[0,0],[1,1],[2,2]])
values = np.array([0,2,8])
zi_griddata = griddata(points, values, (1.5,1.5), method='linear')
This code creates interpolation functions using interp2d and griddata, then estimates values at point (1.5, 1.5).
Execution Table
StepActionInputOutput/Result
1Define x, y arraysx=[0,1,2], y=[0,1,2]Arrays created
2Define z matrixz=[[0,1,4],[1,2,5],[4,5,8]]Matrix created
3Create interp2d functioninterp2d(x,y,z,kind='linear')Function f_interp2d created
4Call f_interp2d(1.5,1.5)x=1.5, y=1.5Interpolated value zi = 4.75
5Define points and values for griddatapoints=[[0,0],[1,1],[2,2]], values=[0,2,8]Arrays created
6Call griddata(points, values, (1.5,1.5), method='linear')point=(1.5,1.5)Interpolated value zi_griddata = 5.0
7EndNo more callsInterpolation complete
💡 All interpolation steps completed, values estimated at (1.5,1.5)
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6Final
xundefined[0,1,2][0,1,2][0,1,2][0,1,2][0,1,2][0,1,2][0,1,2]
yundefined[0,1,2][0,1,2][0,1,2][0,1,2][0,1,2][0,1,2][0,1,2]
zundefinedundefined[[0,1,4],[1,2,5],[4,5,8]][[0,1,4],[1,2,5],[4,5,8]][[0,1,4],[1,2,5],[4,5,8]][[0,1,4],[1,2,5],[4,5,8]][[0,1,4],[1,2,5],[4,5,8]][[0,1,4],[1,2,5],[4,5,8]]
f_interp2dundefinedundefinedundefinedinterp2d functioninterp2d functioninterp2d functioninterp2d functioninterp2d function
ziundefinedundefinedundefinedundefined4.754.754.754.75
pointsundefinedundefinedundefinedundefinedundefined[[0,0],[1,1],[2,2]][[0,0],[1,1],[2,2]][[0,0],[1,1],[2,2]]
valuesundefinedundefinedundefinedundefinedundefined[0,2,8][0,2,8][0,2,8]
zi_griddataundefinedundefinedundefinedundefinedundefinedundefined5.05.0
Key Moments - 3 Insights
Why does interp2d require grid data but griddata can handle scattered points?
interp2d needs x and y arrays to be sorted and form a grid (see Step 3 in execution_table), while griddata works directly with scattered points (Step 5). This is why griddata is more flexible for irregular data.
Why can the interpolated values from interp2d and griddata be different at (1.5, 1.5)?
interp2d uses the grid z values and linear interpolation on the grid (Step 4), while griddata uses the scattered points and their values (Step 6). Different input data and methods cause different results.
What happens if we input a point outside the original data range?
Both methods may return NaN or extrapolate poorly. This is because interpolation estimates values inside known data bounds (see exit_note). Extrapolation is not guaranteed.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 4, what is the interpolated value from interp2d at (1.5, 1.5)?
AUndefined
B5.0
C3.75
D2.5
💡 Hint
Check the Output/Result column at Step 4 in execution_table.
At which step does griddata compute the interpolated value?
AStep 3
BStep 6
CStep 4
DStep 2
💡 Hint
Look for 'griddata' call and output in execution_table rows.
If we change the interpolation method in interp2d from 'linear' to 'cubic', what changes in the execution_table?
AStep 3 function creation fails
BStep 6 output value changes
CStep 4 output value changes
DNo change at all
💡 Hint
interp2d function creation is at Step 3, output at Step 4 depends on method.
Concept Snapshot
2D Interpolation with interp2d and griddata:
- interp2d: for gridded data, creates a function f(x,y)
- griddata: for scattered points, interpolates at any point
- Both estimate values inside known data range
- Use kind='linear' or 'cubic' for smoothness
- Output: interpolated values at new points
Full Transcript
This visual execution shows how 2D interpolation works using scipy's interp2d and griddata. We start with known data points arranged either on a grid or scattered. interp2d requires grid data and creates a function to estimate values at new points. griddata works directly with scattered points and interpolates values at any location. We traced variable values and function calls step-by-step, showing how inputs transform into interpolated outputs. Key moments clarify differences between methods and common beginner questions. The quiz tests understanding of the interpolation steps and results.