The SciPy documentation explains that interp1d's kind argument can take the values ‘linear’, ‘nearest’, ‘zero’, ‘slinear’, ‘quadratic’, ‘cubic’. The last three are spline orders and 'linear' is self-explanatory. What do 'nearest' and 'zero' do?
nearest "snaps" to the nearest data point.zero is a zero order spline. It's value at any point is the last raw value seen.linear performs linear interpolation and slinear uses a first
order spline. They use different code and can produce similar but subtly different results.quadratic uses second order spline interpolation.cubic uses third order spline interpolation.Note that the k parameter can also accept an integer specifying the order of spline interpolation.
import numpy as np
import matplotlib.pyplot as plt
import scipy.interpolate as interpolate
np.random.seed(6)
kinds = ('nearest', 'zero', 'linear', 'slinear', 'quadratic', 'cubic')
N = 10
x = np.linspace(0, 1, N)
y = np.random.randint(10, size=(N,))
new_x = np.linspace(0, 1, 28)
fig, axs = plt.subplots(nrows=len(kinds)+1, sharex=True)
axs[0].plot(x, y, 'bo-')
axs[0].set_title('raw')
for ax, kind in zip(axs[1:], kinds):
    new_y = interpolate.interp1d(x, y, kind=kind)(new_x)
    ax.plot(new_x, new_y, 'ro-')
    ax.set_title(kind)
plt.show()

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With