GRIDDATA
Overview
The GRIDDATA
function performs interpolation on unstructured N-dimensional data. It is useful for estimating values at new points based on scattered data, such as in geospatial analysis or scientific computing. The function supports three interpolation methods: ‘linear’, ‘nearest’, and ‘cubic’. The underlying algorithm is provided by SciPy’s scipy.interpolate.griddata
function. For more details, see the official documentation .
This example function is provided as-is without any representation of accuracy.
Usage
To use the function in Excel:
=GRIDDATA(points, values, xi, [method], [fill_value])
points
(2D list, required): Table of coordinates for the data points. Each row is a point in N dimensions.values
(2D column vector, required): Table of values at each data point. Must have the same number of rows aspoints
.xi
(2D list, required): Table of coordinates where interpolation is desired. Each row is a point in N dimensions.method
(string, optional, default=“linear”): Interpolation method. One of"linear"
,"nearest"
, or"cubic"
.fill_value
(float, optional, default=NaN): Value to use for points outside the convex hull of the input points.
The function returns a 2D column vector (list of lists) of interpolated values at the locations specified by xi
, or an error message (list of lists of strings) if the input is invalid.
Examples
Example 1: Linear Interpolation in 2D
This example interpolates values at two new points using three known data points in 2D.
Inputs:
points | values | xi | method | fill_value | ||
---|---|---|---|---|---|---|
0.0 | 0.0 | 1.0 | 0.5 | 0.5 | linear | |
1.0 | 0.0 | 2.0 | 0.2 | 0.8 | ||
0.0 | 1.0 | 3.0 |
Excel formula:
=GRIDDATA({0,0;1,0;0,1}, {1;2;3}, {0.5,0.5;0.2,0.8}, "linear")
Expected output:
Result |
---|
2.5 |
2.8 |
Example 2: Nearest Neighbor Interpolation
This example uses the ‘nearest’ method to interpolate at two points.
Inputs:
points | values | xi | method | fill_value | ||
---|---|---|---|---|---|---|
0.0 | 0.0 | 1.0 | 0.9 | 0.1 | nearest | |
1.0 | 0.0 | 2.0 | 0.1 | 0.9 | ||
0.0 | 1.0 | 3.0 |
Excel formula:
=GRIDDATA({0,0;1,0;0,1}, {1;2;3}, {0.9,0.1;0.1,0.9}, "nearest")
Expected output:
Result |
---|
2.0 |
3.0 |
Example 3: Cubic Interpolation in 2D
This example uses the ‘cubic’ method for interpolation with four non-collinear points.
Inputs:
points | values | xi | method | fill_value | ||
---|---|---|---|---|---|---|
0.0 | 0.0 | 1.0 | 0.3 | 0.3 | cubic | |
1.0 | 0.0 | 2.0 | 0.7 | 0.7 | ||
0.0 | 1.0 | 3.0 | 0.5 | 0.5 | ||
1.0 | 1.0 | 4.0 |
Excel formula:
=GRIDDATA({0,0;1,0;0,1;1,1}, {1;2;3;4}, {0.3,0.3;0.7,0.7;0.5,0.5}, "cubic")
Expected output:
Result |
---|
1.9 |
3.1 |
2.5 |
Example 4: Fill Value for Out-of-Bounds
This example shows how to use fill_value
for a point outside the convex hull, with four points.
Inputs:
points | values | xi | method | fill_value | ||
---|---|---|---|---|---|---|
0.0 | 0.0 | 1.0 | 2.0 | 2.0 | linear | 99 |
1.0 | 0.0 | 2.0 | ||||
0.0 | 1.0 | 3.0 | ||||
1.0 | 1.0 | 4.0 |
Excel formula:
=GRIDDATA({0,0;1,0;0,1;1,1}, {1;2;3;4}, {2,2}, "linear", 99)
Expected output:
Result |
---|
99 |
Python Code
from scipy.interpolate import griddata as scipy_griddata
import numpy as np
def griddata(points, values, xi, method="linear", fill_value=None):
"""
Interpolate unstructured N-D data using scipy.interpolate.griddata.
Args:
points: 2D list of coordinates for data points (shape: n_points x n_dim).
values: 2D column vector of values at each point (shape: n_points x 1).
xi: 2D list of coordinates where interpolation is desired (shape: n_xi x n_dim).
method: Interpolation method: 'linear', 'nearest', or 'cubic' (default: 'linear').
fill_value: Value to use for points outside the convex hull (default: None).
Returns:
2D column vector (list of lists) of interpolated values.
Note: Only valid 2D lists are supported. Examples and demo cases do not include invalid or error cases.
"""
# Validate input shapes
try:
pts = np.array(points, dtype=float)
vals = np.array(values, dtype=float).flatten()
xis = np.array(xi, dtype=float)
if pts.ndim != 2 or xis.ndim != 2:
return [["Invalid input: points and xi must be 2D lists."]]
if vals.ndim != 1 or pts.shape[0] != vals.shape[0]:
return [["Invalid input: values must be a column vector with same number of rows as points."]]
if pts.shape[1] != xis.shape[1]:
return [["Invalid input: points and xi must have same number of columns."]]
if method not in ["linear", "nearest", "cubic"]:
return [["Invalid input: method must be 'linear', 'nearest', or 'cubic'."]]
# Use scipy_griddata default fill_value (nan) when no fill_value provided
if fill_value is None:
result = scipy_griddata(pts, vals, xis, method=method)
else:
result = scipy_griddata(pts, vals, xis, method=method, fill_value=fill_value)
# Return as 2D column vector, None for np.nan
return [[float(x)] if np.isfinite(x) else [None] for x in result]
except Exception as e:
return [[f"Error: {e}"]]
Live Notebook
Edit this function in a live notebook .