
- SciPy - Home
- SciPy - Introduction
- SciPy - Environment Setup
- SciPy - Basic Functionality
- SciPy - Relationship with NumPy
- SciPy Clusters
- SciPy - Clusters
- SciPy - Hierarchical Clustering
- SciPy - K-means Clustering
- SciPy - Distance Metrics
- SciPy Constants
- SciPy - Constants
- SciPy - Mathematical Constants
- SciPy - Physical Constants
- SciPy - Unit Conversion
- SciPy - Astronomical Constants
- SciPy - Fourier Transforms
- SciPy - FFTpack
- SciPy - Discrete Fourier Transform (DFT)
- SciPy - Fast Fourier Transform (FFT)
- SciPy Integration Equations
- SciPy - Integrate Module
- SciPy - Single Integration
- SciPy - Double Integration
- SciPy - Triple Integration
- SciPy - Multiple Integration
- SciPy Differential Equations
- SciPy - Differential Equations
- SciPy - Integration of Stochastic Differential Equations
- SciPy - Integration of Ordinary Differential Equations
- SciPy - Discontinuous Functions
- SciPy - Oscillatory Functions
- SciPy - Partial Differential Equations
- SciPy Interpolation
- SciPy - Interpolate
- SciPy - Linear 1-D Interpolation
- SciPy - Polynomial 1-D Interpolation
- SciPy - Spline 1-D Interpolation
- SciPy - Grid Data Multi-Dimensional Interpolation
- SciPy - RBF Multi-Dimensional Interpolation
- SciPy - Polynomial & Spline Interpolation
- SciPy Curve Fitting
- SciPy - Curve Fitting
- SciPy - Linear Curve Fitting
- SciPy - Non-Linear Curve Fitting
- SciPy - Input & Output
- SciPy - Input & Output
- SciPy - Reading & Writing Files
- SciPy - Working with Different File Formats
- SciPy - Efficient Data Storage with HDF5
- SciPy - Data Serialization
- SciPy Linear Algebra
- SciPy - Linalg
- SciPy - Matrix Creation & Basic Operations
- SciPy - Matrix LU Decomposition
- SciPy - Matrix QU Decomposition
- SciPy - Singular Value Decomposition
- SciPy - Cholesky Decomposition
- SciPy - Solving Linear Systems
- SciPy - Eigenvalues & Eigenvectors
- SciPy Image Processing
- SciPy - Ndimage
- SciPy - Reading & Writing Images
- SciPy - Image Transformation
- SciPy - Filtering & Edge Detection
- SciPy - Top Hat Filters
- SciPy - Morphological Filters
- SciPy - Low Pass Filters
- SciPy - High Pass Filters
- SciPy - Bilateral Filter
- SciPy - Median Filter
- SciPy - Non - Linear Filters in Image Processing
- SciPy - High Boost Filter
- SciPy - Laplacian Filter
- SciPy - Morphological Operations
- SciPy - Image Segmentation
- SciPy - Thresholding in Image Segmentation
- SciPy - Region-Based Segmentation
- SciPy - Connected Component Labeling
- SciPy Optimize
- SciPy - Optimize
- SciPy - Special Matrices & Functions
- SciPy - Unconstrained Optimization
- SciPy - Constrained Optimization
- SciPy - Matrix Norms
- SciPy - Sparse Matrix
- SciPy - Frobenius Norm
- SciPy - Spectral Norm
- SciPy Condition Numbers
- SciPy - Condition Numbers
- SciPy - Linear Least Squares
- SciPy - Non-Linear Least Squares
- SciPy - Finding Roots of Scalar Functions
- SciPy - Finding Roots of Multivariate Functions
- SciPy - Signal Processing
- SciPy - Signal Filtering & Smoothing
- SciPy - Short-Time Fourier Transform
- SciPy - Wavelet Transform
- SciPy - Continuous Wavelet Transform
- SciPy - Discrete Wavelet Transform
- SciPy - Wavelet Packet Transform
- SciPy - Multi-Resolution Analysis
- SciPy - Stationary Wavelet Transform
- SciPy - Statistical Functions
- SciPy - Stats
- SciPy - Descriptive Statistics
- SciPy - Continuous Probability Distributions
- SciPy - Discrete Probability Distributions
- SciPy - Statistical Tests & Inference
- SciPy - Generating Random Samples
- SciPy - Kaplan-Meier Estimator Survival Analysis
- SciPy - Cox Proportional Hazards Model Survival Analysis
- SciPy Spatial Data
- SciPy - Spatial
- SciPy - Special Functions
- SciPy - Special Package
- SciPy Advanced Topics
- SciPy - CSGraph
- SciPy - ODR
- SciPy Useful Resources
- SciPy - Reference
- SciPy - Quick Guide
- SciPy - Cheatsheet
- SciPy - Useful Resources
- SciPy - Discussion
SciPy - interpolate.RegularGridInterpolator() Function
scipy.interpolate.RegularGridInterpolator() is a function in SciPy designed for interpolating data on a regular grid in one or more dimensions. It creates an interpolation function based on values defined at grid points.
When users specify the grid coordinates and corresponding values and the interpolator then can compute values at any point within the grid using linear or nearest-neighbor interpolation methods.
It is efficient for multidimensional data and is particularly useful in applications such as numerical simulations where data is regularly spaced. This interpolator can also handle extrapolation beyond the grid boundaries by specifying a fill value.
Syntax
Following is the syntax of the function scipy.interpolate.RegularGridInterpolator() which is used to perform interpolation−
scipy.interpolate.RegularGridInterpolator(points, values, method='linear', bounds_error=True, fill_value=np.nan)
Parameters
Following are the parameters of the scipy.interpolate.RegularGridInterpolator() function −
- points: A sequence of 1D arrays defining the coordinates of the grid points in each dimension. The length of this sequence should be equal to the number of dimensions.
- values: An array of shape N_1, N_2, ..., N_d where N_i is the number of points along the i-th dimension. This array contains the values at the grid points defined by points.
- method: This is the interpolation method. The interpolation methods such as linear, nearest and we can also specify higher-order methods if desired.
- bounds_error: If True (default) then an error is raised when interpolation is requested for points outside the grid boundaries. If False then fill_value is used for out-of-bounds points.
- fill_value: Value to use for points outside the grid if bounds_error is False. The default value is np.nan.
Return Value
The scipy.interpolate.RegularGridInterpolator() function returns the interpolated values at the defined points.
1D Interpolation
Following is the example of scipy.interpolate.RegularGridInterpolator() function in which well 1-d interpolate −
import numpy as np from scipy.interpolate import RegularGridInterpolator import matplotlib.pyplot as plt # Define a 1D grid x = np.linspace(0, 10, 11) # x-coordinates (0, 1, 2, ..., 10) # Define values on this grid (e.g., a function of x) values = np.sin(x) # Sample values (sine function) # Create a RegularGridInterpolator object interp_func = RegularGridInterpolator((x,), values) # Points to interpolate at (these can be any value within the range of x) xi = np.array([0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5]) # Perform interpolation interpolated_values = interp_func(xi) # Print interpolated values print("Interpolated values:", interpolated_values) # Visualization plt.plot(x, values, 'o', label='Data points (sin(x))') plt.plot(xi, interpolated_values, 'x', label='Interpolated values', color='red') plt.xlabel('x') plt.ylabel('sin(x)') plt.title('1D Interpolation using RegularGridInterpolator') plt.legend() plt.grid() plt.show()
Here is the output of the scipy.interpolate.RegularGridInterpolator() function used for simple 1d interpolation −
Interpolated values: [ 0.42073549 0.87538421 0.52520872 -0.30784124 -0.85786338 -0.61916989 0.18878555 0.82317242 0.70073837 -0.06595131]

2D Interpolation with Custom Values
In this example we can perform 2D interpolation using scipy.interpolate.RegularGridInterpolator() with custom values in which we define a custom grid and use the interpolator to obtain values at arbitrary points −
import numpy as np from scipy.interpolate import RegularGridInterpolator import matplotlib.pyplot as plt # Define 2D grid points x = np.linspace(0, 5, 6) # x-coordinates (0, 1, 2, 3, 4, 5) y = np.linspace(0, 5, 6) # y-coordinates (0, 1, 2, 3, 4, 5) # Define custom values on the grid (a 2D function) # For example, a simple function: z = f(x, y) = sin(sqrt(x^2 + y^2)) X, Y = np.meshgrid(x, y) # Create a meshgrid for x and y Z = np.sin(np.sqrt(X**2 + Y**2)) # Compute custom values # Create the RegularGridInterpolator object interp_func = RegularGridInterpolator((x, y), Z) # Define points where we want to interpolate xi = np.array([[1.5, 1.5], [2.5, 2.5], [3.5, 3.5], [4.2, 1.8], [0.5, 0.5]]) # Perform interpolation interpolated_values = interp_func(xi) # Print interpolated values print("Interpolated values at specified points:", interpolated_values) # Visualization # Create a grid for visualization xi_grid, yi_grid = np.meshgrid(np.linspace(0, 5, 50), np.linspace(0, 5, 50)) zi_grid = interp_func(np.array([xi_grid.ravel(), yi_grid.ravel()]).T).reshape(xi_grid.shape) plt.figure(figsize=(10, 6)) plt.contourf(xi_grid, yi_grid, zi_grid, levels=50, cmap='viridis') plt.colorbar(label='Interpolated Values') plt.scatter(xi[:, 0], xi[:, 1], color='red', label='Interpolation Points', zorder=5) plt.title('2D Interpolation using RegularGridInterpolator with Custom Values') plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.legend() plt.show()
Here is the output of the scipy.interpolate.RegularGridInterpolator() function used for 2d interpolation −
Interpolated values at specified points: [ 0.71733399 -0.3696485 -0.84892675 -0.91681464 0.66767698]
