Two-dimensional interpolation with scipy.interpolate.RectBivariateSpline

In the following code, the function $$ z(x,y) = e^{-4x^2}e^{-y^2/4} $$ is calculated on a regular, coarse grid and then interpolated onto a finer one.

import numpy as np
from scipy.interpolate import RectBivariateSpline
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# Regularly-spaced, coarse grid
dx, dy = 0.4, 0.4
xmax, ymax = 2, 4
x = np.arange(-xmax, xmax, dx)
y = np.arange(-ymax, ymax, dy)
X, Y = np.meshgrid(x, y)
Z = np.exp(-(2*X)**2 - (Y/2)**2)

interp_spline = RectBivariateSpline(y, x, Z)

# Regularly-spaced, fine grid
dx2, dy2 = 0.16, 0.16
x2 = np.arange(-xmax, xmax, dx2)
y2 = np.arange(-ymax, ymax, dy2)
X2, Y2 = np.meshgrid(x2,y2)
Z2 = interp_spline(y2, x2)

fig, ax = plt.subplots(nrows=1, ncols=2, subplot_kw={'projection': '3d'})
ax[0].plot_wireframe(X, Y, Z, color='k')

ax[1].plot_wireframe(X2, Y2, Z2, color='k')
for axes in ax:


Note that for our function, Z, defined using the meshgrid set up here, the RectBivariateSpline method expects the corresponding one-dimensional arrays y and x to be passed in this order (opposite to that of interp2d.) This issue is related to the way that meshgrid is indexed which is based on the conventions of MATLAB.

Interpolation with scipy.interpolate.RectBivariateSpline