Interpolate 2d data-points to a given coordinate grid. This is useful when a 2d plot is needed but the data points are sparse or the coordinates are not integers values. In order to use it, one needs to build 3+2 arrays, i.e. the x, y, and z (the value) coordinates of the dataset and the grid. They can easily be feeded to the griddata function as follows: griddata((x,y), z, (grid_x, grid_y)).

The grid must be 2d and to build it a possible method is to use np.linspace (or any other sequence) and feed the griddata function with ..., (linspace_x[None,:], linspace_y[:,None]).

Griddata returns a 2d array that contains the interpolated value of z corresponding to the grid points.

import matplotlib.pyplot as plt
import as cm
from scipy.interpolate import griddata
import numpy as np

%matplotlib inline

fig, ax = plt.subplots(1, 3, figsize=(10,3.5))

yx = np.mgrid[:50, :50]
z = (np.cos(yx[1]/8)+np.sin(yx[0]/4)+2)/4

xs, ys, zs = [], [], []

for x, y in zip(*np.random.randint(low=0, high=50, size=(2, 200))):
    zs.append(z[y, x])

ax[1].scatter(xs, ys, color=cm.viridis(z[ys, xs]))

x_grid = np.linspace(0,50, 20)
y_grid = np.linspace(0, 50, 20)
data_in_grid = griddata((xs, ys), zs, (x_grid[None,:], y_grid[:,None]), method='linear')

CS = ax[2].contour(x_grid, y_grid, data_in_grid, 10,
                    linewidths=0.5, colors='white', alpha=0.5)
CF = ax[2].contourf(x_grid, y_grid, data_in_grid, 10)

<?xml version=”1.0” encoding=”utf-8” standalone=”no”?>