Parametric interpolation helps you find smooth curves through points when both x and y depend on a parameter. It is useful to create smooth paths or shapes.
0
0
Parametric interpolation in SciPy
Introduction
You want to draw a smooth curve through a set of points that form a shape.
You need to model a path where x and y change with time or another parameter.
You want to fill in missing points smoothly between known points in 2D or 3D.
You want to animate an object moving smoothly along a path defined by points.
Syntax
SciPy
from scipy.interpolate import splprep, splev # points: array of shape (2, n) for 2D points # s: smoothness parameter (0 for interpolation) # k: degree of spline (usually 3) tck, u = splprep(points, s=0, k=3) # u_new: new parameter values to evaluate spline new_points = splev(u_new, tck)
splprep creates a parametric spline representation from points.
splev evaluates the spline at new parameter values.
Examples
This example creates a smooth curve through 4 points in 2D and evaluates 100 points on the curve.
SciPy
from scipy.interpolate import splprep, splev import numpy as np points = np.array([[0, 1, 2, 3], [0, 1, 0, 1]]) tck, u = splprep(points, s=0) u_new = np.linspace(0, 1, 100) new_points = splev(u_new, tck)
Here, smoothing is applied (s=1) and a quadratic spline (k=2) is used for a smoother curve.
SciPy
tck, u = splprep(points, s=1, k=2) new_points = splev(np.linspace(0, 1, 50), tck)
Sample Program
This program creates a smooth curve through given 2D points using parametric interpolation. It plots the original points as red dots and the smooth curve as a blue line.
SciPy
from scipy.interpolate import splprep, splev import numpy as np import matplotlib.pyplot as plt # Define 2D points forming a rough circle points = np.array([ [0, 1, 2, 3, 4, 5, 6], [0, 2, 1, 3, 1, 2, 0] ]) # Create parametric spline with no smoothing tck, u = splprep(points, s=0, k=3) # Generate new parameter values u_new = np.linspace(0, 1, 200) # Evaluate spline to get smooth curve points smooth_points = splev(u_new, tck) # Plot original points and smooth curve plt.plot(points[0], points[1], 'ro', label='Original Points') plt.plot(smooth_points[0], smooth_points[1], 'b-', label='Smooth Curve') plt.legend() plt.title('Parametric Interpolation with splprep and splev') plt.xlabel('X') plt.ylabel('Y') plt.grid(True) plt.show()
OutputSuccess
Important Notes
Parametric interpolation works well when x and y depend on a parameter like time.
Setting s=0 forces the curve to pass through all points exactly.
Higher k values create smoother curves but need more points.
Summary
Parametric interpolation finds smooth curves through points using a parameter.
Use splprep to create the spline and splev to evaluate it.
This method is great for smooth paths and shapes in 2D or 3D.