SciPy - interpolate.PchipInterpolator() Function



scipy.interpolate.PchipInterpolator() is a monotonic spline interpolation method in Python's SciPy library. It constructs a piecewise cubic Hermite interpolating polynomial (PCHIP) that preserves the shape and monotonicity of the input data by making it ideal for smoothly interpolating data without overshooting especially for non-oscillating datasets.

When compared to spline interpolation PCHIP avoids oscillations between points ensuring that intermediate values respect the trends of the original data. This function takes x and y values as input by creating an interpolator object that can evaluate new y-values at any intermediate x-points. This is especially useful in data visualization and numerical analysis.

Syntax

Following is the syntax of the function scipy.interpolate.PchipInterpolator() to perform monotonic spline interpolation −

PchipInterpolator(x, y, axis=0, extrapolate=None)

Parameters

Here are the parameters of the scipy.interpolate.PchipInterpolator() function −

  • x: The 1-dimensional array of x-coordinates which must be strictly increasing.
  • y: The array of y-coordinates corresponding to each x value.
  • axis: This parameter specifies the axis in y to which interpolation is applied. Default value is 0.
  • extrapolate: This parameter controls extrapolation behavior for values outside the x range. If None then extrapolation is allowed and if False then it raises an error for values beyond the range.

Return Value

The scipy.interpolate.PchipInterpolator() function returns an interpolator object that can be used to evaluate the interpolated values at any desired x-coordinates within the range of the input data.

Basic Interpolation

Following is the example of the scipy.interpolate.PchipInterpolator() function which is used to perform monotonic spline interpolation. Here in this example given some data points to create a PCHIP interpolator to estimate values between them smoothly −

from scipy.interpolate import PchipInterpolator
import numpy as np
import matplotlib.pyplot as plt

# Define data points
x = [0, 1, 2, 3, 4]
y = [0, 1, 0, 1, 0]

# Create the interpolator
pchip = PchipInterpolator(x, y)

# Generate points for a smooth curve
x_new = np.linspace(0, 4, 100)
y_new = pchip(x_new)

# Plot
plt.plot(x, y, 'o', label='Original points')
plt.plot(x_new, y_new, '-', label='PCHIP interpolation')
plt.legend()
plt.show()

Here is the output of the scipy.interpolate.PchipInterpolator() function −

Pchip Interpolator basic Example

Monotonic Data Interpolation

When interpolating monotonic data PchipInterpolator() function is particularly effective because it ensures the interpolated values stay within the range and trend of the data without oscillations or overshooting. This makes it ideal for smooth transitions in cumulative or sequential data. Below is the example for our better understanding −

from scipy.interpolate import PchipInterpolator
import numpy as np
import matplotlib.pyplot as plt

# Define monotonic data points (increasing trend)
x = [0, 1, 2, 3, 4, 5]
y = [0, 0.5, 0.75, 0.8, 0.9, 1.0]

# Create the PCHIP interpolator
pchip = PchipInterpolator(x, y)

# Generate smooth x-points for interpolation
x_new = np.linspace(0, 5, 100)
y_new = pchip(x_new)

# Plot the original points and interpolated curve
plt.plot(x, y, 'o', label='Original points')
plt.plot(x_new, y_new, '-', label='Monotonic PCHIP interpolation')
plt.xlabel('X')
plt.ylabel('Y')
plt.legend()
plt.title('Monotonic Data Interpolation using PCHIP')
plt.show()

Here is the output of the scipy.interpolate.PchipInterpolator() function used on monotonic data −

Pchip Interpolator with Monotonic data

Interpolating and Differentiating

Interpolating and differentiating with scipy.interpolate.PchipInterpolator() function involves creating a smooth interpolation from data points and then obtaining derivatives of this interpolated function. This is particularly useful for analyzing the rate of change in data or smoothing noisy data. Following is the example −

import numpy as np
from scipy.interpolate import PchipInterpolator
import matplotlib.pyplot as plt

# Sample data points
x = np.array([0, 1, 2, 3, 4, 5])
y = np.array([0, 0.5, 2, 1.5, 0.5, 0])

# Create the PCHIP interpolator
pchip = PchipInterpolator(x, y)

# Define new x values for interpolation
x_new = np.linspace(0, 5, 100)
y_new = pchip(x_new)

# Calculate the first derivative using the derivative method
y_derivative = pchip.derivative()(x_new)

# Plot the original data, interpolated values, and the derivative
plt.figure(figsize=(10, 6))

# Original data and interpolation plot
plt.subplot(2, 1, 1)
plt.plot(x, y, 'o', label='Original data')
plt.plot(x_new, y_new, label='PCHIP Interpolation')
plt.title('Interpolation and Derivative using PCHIP')
plt.legend()

# Derivative plot
plt.subplot(2, 1, 2)
plt.plot(x_new, y_derivative, 'r', label="PCHIP Derivative")
plt.xlabel('x')
plt.ylabel("y'")
plt.legend()

plt.tight_layout()
plt.show()

Here is the output of the scipy.interpolate.PchipInterpolator() function with Interpolation and Derivatives −

Pchip Interpolator with Inetrpolation and Derivative
scipy_interpolate.htm
Advertisements