Interpolate

interp1d on temps.

interp1d builds a callable through samples. Nairobi temps in the comments are °C.

Goal

Interpolate a short temperature series and print a mid-day value.

from scipy.interpolate import interp1d
hours = np.array([0.0, 6, 12, 18])
temps = np.array([14.0, 16, 24, 18])  # Nairobi-ish
f = interp1d(hours, temps, kind='linear')
print(float(f(9)), float(f(15)))
from scipy.interpolate import interp1d
x = np.array([0.0, 1, 2, 3])
y = np.array([80.0, 60, 110, 90])
f = interp1d(x, y, kind='cubic')
xs = np.linspace(0, 3, 7)
print(f(xs).round(1))
from scipy.interpolate import interp1d
x = np.linspace(0, 2 * np.pi, 8)
y = np.sin(x)
f = interp1d(x, y, kind='cubic')
plt.plot(x, y, 'o')
xs = np.linspace(0, 2 * np.pi, 80)
plt.plot(xs, f(xs))
plt.show()
print('ok')
from scipy.interpolate import interp1d
try:
    interp1d([0, 1], [1, 2])(5)
except ValueError as err:
    print(type(err).__name__)