I donβt think there is a very simple way, mainly because you want to mix raster and vector data. Fortunately, Matplotlib paths have the ability to check if a point is in the path by doing this for all pixels, creating a mask, but I think this method can be very slow for large datasets.
import matplotlib.patches as patches from matplotlib.nxutils import points_inside_poly import matplotlib.pyplot as plt import numpy as np
Please note that the contour lines, which by default "leave" the graph on different edges, do not make the path following these edges. These lines will require additional processing.

source share