# scipy.interpolate.interp2d

In the following example, we calculate the function $$z(x,y) = \sin\left(\frac{\pi x}{2}\right)e^{y/2}$$ on a grid of points $(x,y)$ which is not evenly-spaced in the $y$-direction. We then use scipy.interpolate.interp2d to interpolate these values onto a finer, evenly-spaced $(x,y)$ grid.

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

x = np.linspace(0, 4, 13)
y = np.array([0, 2, 3, 3.5, 3.75, 3.875, 3.9375, 4])
X, Y = np.meshgrid(x, y)
Z = np.sin(np.pi*X/2) * np.exp(Y/2)

x2 = np.linspace(0, 4, 65)
y2 = np.linspace(0, 4, 65)
f = interp2d(x, y, Z, kind='cubic')
Z2 = f(x2, y2)

fig, ax = plt.subplots(nrows=1, ncols=2)
ax[0].pcolormesh(X, Y, Z)

X2, Y2 = np.meshgrid(x2, y2)
ax[1].pcolormesh(X2, Y2, Z2)

plt.show()


Note that interp2d requires the one-dimensional arrays, x and y.