0

pcolormesh plots only exterior coordinates, required plot is

enter image description here

newPress=[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[0]=(-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)

poly3[1]=(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)
xi, yi = np.meshgrid(xi, yi)
x, y, z = poly3[0], poly3[1], newPress
zi = griddata(x, y, z, xi, yi,interp='linear')

fig2 = plt.figure(figsize=(8, 3.5))
ax2 = fig2.add_subplot(111)
ax2.scatter(x,y)
m = plt.pcolormesh(xi, yi, zi, alpha=0.15, cmap='viridis_r')
plt.show()
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Sudhakar
  • 30
  • 8

1 Answers1

0

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:

result of the above code

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:

result of the second patch of code

Thomas Kühn
  • 9,412
  • 3
  • 47
  • 63
  • 1
    Thomas Kuhn - that was great. Thanks for that. My apologies for missing the import statements. – Sudhakar Jul 11 '18 at 08:55
  • Kuhn - the edited code by you is producing different results for me. Is that the complete code? – Sudhakar Jul 12 '18 at 10:00
  • @user4993617 different in what respect? It is the complete code. As I said, your code was not runnable, so I had to *assume* a lot of things. For instance what `imports` to use, etc. Maybe there are some differences? – Thomas Kühn Jul 12 '18 at 10:42
  • Thinking about it, it's probably this line: `ll,lr,ur,ul = zip(x[[2,6,9,12]],y[[2,6,9,12]])`. Maybe your coordinate pairs are ordered differently? – Thomas Kühn Jul 12 '18 at 10:47
  • well i'm using the same arrays of poly3, as posted by you. I'm getting a rectangle, bounding (min and max) of x and y axis. – Sudhakar Jul 12 '18 at 12:52
  • @user4993617 That's strange. Just for double checking -- can you post the full code that you are running and the image you get as an edit below your original question? I checked my code on both Python 2 and 3 and the results are identical. – Thomas Kühn Jul 12 '18 at 12:56
  • @user4993617 yes, but I need to see the code also -- there must be a difference. Either it is in the code or it is a version conflict and the best way to go about fixing these kinds of problems is by systematically going through all possible places where something can go wrong.... – Thomas Kühn Jul 12 '18 at 13:03
  • @user4993617 ok, I ran the code and I get the correct result (the one I post in my answer). Can you tell me what platform you are running on and what version of Python, matplotlib, numpy, and scipy you are using? Possibly also how you installed the software. – Thomas Kühn Jul 12 '18 at 13:12
  • @user4993617 Also, is this part of some bigger piece of code? – Thomas Kühn Jul 12 '18 at 13:13
  • Anaconda 4.2; Python 3.5.2 numpy 1.11.1 matplotlib 1.5.3 scipy 0.18.1 Yes, this will code will be a part of a bigger code. currently, I'm testing it independently. – Sudhakar Jul 12 '18 at 14:30
  • @user4993617 Hmm, you're using really old versions, I have numpy 1.14.5, matplotlib 2.2.2, and scipy 1.1.0. I'll have a look if there are any bigger changes in the functions used between the versions. – Thomas Kühn Jul 12 '18 at 16:24
  • Kuhn - it works after updating the packages. Thanks a tonne. – Sudhakar Jul 13 '18 at 00:05
  • np.logical_and will not work well, if i'm having to deal with varying polygon shapes. is there a way to deal without trunking? – Sudhakar Jul 13 '18 at 01:03
  • [i got a generic way to go around, working code in the link](http://txt.do/dzpkf) – Sudhakar Jul 13 '18 at 09:13