Judging from this part of the scipy.interpolate.griddata
documentation, griddata
appears to interpolate on a convex hull around your data points:
fill_value : float, optional Value used to fill in for requested
points outside of the convex hull of the input points. If not
provided, then the default is nan. This option has no effect for the
‘nearest’ method.
This means that you always will get data also outside of the points you provide to griddata
. In order to not show these areas, you can set the according points to an invalid (np.nan
) value. In your case this is pretty simple.
from matplotlib import pyplot as plt
from scipy.interpolate import griddata
import numpy as np
newPress=np.asarray([22.640048521269733, 8.7880990280388946, 8.5228130097742358, 6.1368312788828003, -0.012232139892299099,
-0.0085282865280444931, 1.4163311525005766, 0.62047309770660242, 14.472422590937441, 15.268280645731416,
17.653997267541644, 24.760479124815305, 22.374762503005076, 22.640048521269733])
poly3 = np.asarray([
(
-15.394, -15.394, -14.394, -14.394, 8.784995481927707,
12.394, 12.394, 15.394, 15.394, 12.394, 12.394, -14.394,
-14.394, -15.394
),
(
13.0625, -13.0625, -13.0625, -17.5625, -17.5625, -15.74980786686838,
-13.0625, -13.0625, 13.0625, 13.0625, 17.562, 17.562, 13.0625, 13.0625
)
])
numcols, numrows = 200, 200
xi = np.linspace(min(poly3[0]), max(poly3[0]), numcols)
yi = np.linspace(min(poly3[1]), max(poly3[1]), numrows)
x, y, z = poly3[0], poly3[1], newPress
xi, yi = np.meshgrid(xi, yi)
zi = griddata(poly3.T,z.T,np.asarray([xi,yi]).T, method='linear').T
fig2 = plt.figure(figsize=(8, 3.5))
ax2 = fig2.add_subplot(111)
ax2.scatter(x,y)
##finding the corners:
ll,lr,ur,ul = zip(x[[2,6,9,12]],y[[2,6,9,12]])
##removing data:
zi[np.logical_and(xi<ll[0],yi<ll[1])] = np.nan
zi[np.logical_and(xi>lr[0],yi<lr[1])] = np.nan
zi[np.logical_and(xi>ur[0],yi>ur[1])] = np.nan
zi[np.logical_and(xi<ul[0],yi>ul[1])] = np.nan
m = ax2.pcolormesh(xi, yi, zi, alpha=0.15, cmap='viridis_r')
plt.show()
The result looks like this:

Please note that your example code was not runnable and needed quite some adjustment. Please, next time when you ask a question, make sure that your code is a Minimal, Complete, and Verifiable example.
EDIT:
For arbitrarily shaped polygons, you can use the technique I outlined here, where you define a path that is composed of your polygon and the outline of the plot area, which you then overlay over your pcolormesh plot with a white color. Note that, in order for this to work properly, your polygon edges have to be ordered correctly along the outline of the polygon (I indicated this in the left subplot in the figure below). In the example below, the edges are ordered in a mathematically positive sense (counter-clockwise) and the edges of the plot area are ordered in the opposite way (clockwise):
from matplotlib import pyplot as plt
from scipy.interpolate import griddata
import numpy as np
from matplotlib.patches import Path, PathPatch
##generate an example polygon:
n_edges = 10
x_max = 15
y_max = 15
##note the sorting of the theta values
thetas = 2*np.pi*np.sort(np.random.rand(n_edges))
radii = 0.5*(np.random.rand(len(thetas))+1)
x = np.cos(thetas)*x_max*radii
y = np.sin(thetas)*y_max*radii
values = np.random.rand(len(thetas))
fig, axes = plt.subplots(ncols=2)
##visualisation
axes[0].quiver(
x[:-1],y[:-1],x[1:]-x[:-1],y[1:]-y[:-1],
scale_units='xy',angles='xy',scale=1,
lw = 3
)
axes[0].scatter(x,y,c=values,zorder=10,cmap='viridis_r')
##interpolation:
numcols, numrows = 200, 200
xi = np.linspace(min(x), max(x), numcols)
yi = np.linspace(min(y), max(y), numrows)
z = values
poly3 = np.asarray([x,y])
xi, yi = np.meshgrid(xi, yi)
zi = griddata(poly3.T,z.T,np.asarray([xi,yi]).T, method='linear').T
axes[1].scatter(x,y, zorder=10)
m = axes[1].pcolormesh(xi, yi, zi, alpha=0.15, cmap='viridis_r',zorder=8)
##getting the limits of the map:
x0,x1 = axes[1].get_xlim()
y0,y1 = axes[1].get_ylim()
map_edges = np.array([[x0,y0],[x0,y1],[x1,y1],[x1,y0]])
##masking the outsides of the polygon
mask = [map_edges,poly3.T]
codes = [[Path.MOVETO] + [Path.LINETO for p in m[1:]] for m in mask]
codes = np.concatenate(codes)
verts = np.concatenate(mask)
path = Path(verts,codes)
patch = PathPatch(path,facecolor='white', lw=0,zorder=9)
axes[1].add_patch(patch)
plt.show()
The result looks something like this:
