title: Contour irregular data within polygon
body: <p>I need to create filled contour plots of sea surface temperature (SST) data within a polygon, however I am not sure the best way to do this. I have three 1D arrays containing data for X, Y, and SST which I plot using the following to create the attached plot: </p>
  
  <pre><code>p=PatchCollection(mypatches,color='none', alpha=1.0,edgecolor="purple",linewidth=2.0)
  levels=np.arange(SST.min(),SST.max(),0.2)
  datamap=mymap.scatter(x,y,c=SST, s=55, vmin=-2,vmax=3,alpha=1.0)
  </code></pre>
  
  <p>I would like to be able to plot these data as filled contours (<a href="http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.contourf" rel="nofollow noreferrer">contourf</a> instead of scatter) that are constrained (clipped) within the polygon boundaries (the purple line). Suggestions for how to achieve this are greatly appreciated.</p>
  
  <p><img src="https://i.stack.imgur.com/EkpUZ.png" alt="enter image description here"></p>
  
  <p><strong>Update:</strong>
  I had originally tried griddata, but could not get it to work properly. However, based on the answer provided by @eatHam I decided to try again. I could not get my scipy griddata to work as it kept hanging at the gridding when selecting method 'cubic', however when I switched to matplotlib.mlab.griddata and used 'linear' interpolation it worked. The suggestion for masking the boundaries provided a very coarse and not as exact solution as i would prefer.
  <img src="https://i.stack.imgur.com/ZIQ2z.png" alt="Image showing the solution using masked clipping"></p>
  
  <p>I searched for options on how to clip contours in matplotlib and I found an answer by @pelson at this <a href="http://matplotlib.1069221.n5.nabble.com/Clipping-a-plot-inside-a-polygon-td41950.html" rel="nofollow noreferrer">link</a>. I tried the suggested solution implied in: "The contour set itself does not have a set_clip_path method but you can iterate over each of the contour collections and set their respective clip paths". My new and final solution looks like this (see plot below):</p>
  
  <pre><code>  p=PatchCollection(mypatches,color='none', alpha=1.0,edgecolor="purple",linewidth=2.0)
    levels=np.arange(SST.min(),SST.max(),0.2)
    grid_x, grid_y = np.mgrid[x.min()-0.5*(x.min()):x.max()+0.5*(x.max()):200j,
                            y.min()-0.5*(y.min()):y.max()+0.5*(y.max()):200j]
    grid_z = griddata(x,y,SST,grid_x,grid_y)
  
    cs=mymap.contourf(grid_x, grid_y, grid_z)
  
    for poly in mypatches:
        for artist in ax.get_children():
            artist.set_clip_path(poly)
  
        ax.add_patch(poly)
    mymap.drawcountries()
    mymap.drawcoastlines()
    mymap.fillcontinents(color='lightgrey',lake_color='none')
    mymap.drawmapboundary(fill_color='none')
  </code></pre>
  
  <p>This solution could also be improved particularily in terms of extrapolating the extreme edges in the North. Suggestions for how to really 'fill-in' the full polygon are appreciated. I also would like to understand why mlab worked and scipy not.</p>
  
  <p><img src="https://i.stack.imgur.com/g4vVp.png" alt="Final solution showing clipped contours using set_clip_path"></p>