title: Python irregular x,y data to contour plot on original domain
body: <p>I have file containing points under the columns "x-cord", "y-cord", "value". These are irregularly spaced. I am trying to make a contour plot of "value" and overlay this over the original domain. I gave up trying to do this in both pgfplots and matlab and thought I would give python a go. An answer in any of these scripts would be fine. The python script is as follows</p>
  
  <pre><code>import numpy as np
  from scipy.interpolate import griddata
  import matplotlib.pyplot as plt
  import numpy.ma as ma
  from numpy.random import uniform, seed
  from scipy.spatial import ConvexHull
  #
  # Loading data
  filename = "strain.dat"
  coordinates = []
  x_c = []
  y_c = []
  z_c = []
  xyz = open(filename)
  title = xyz.readline()
  for line in xyz:
      x,y,z = line.split()
      coordinates.append([float(x), float(y), float(z)])
      x_c.append([float(x)])
      y_c.append([float(y)])
      z_c.append([float(z)])
  xyz.close()
  #
  # Rehaping and translating data
  x_c=np.ravel(np.array(x_c))
  y_c=np.ravel(np.array(y_c))
  z_c=np.ravel(np.array(z_c))
  x_c = x_c-100.0
  y_c = y_c-100.0
  #
  # Checking the convex hull
  points=np.column_stack((x_c,y_c))
  hull = ConvexHull(points);
  plt.plot(points[hull.vertices,0], points[hull.vertices,1], 'r--', lw=2)
  plt.scatter(x_c, y_c, marker='o', s=5, zorder=10)
  #
  # Mapping the irregular data onto a regular grid and plotting
  xic = np.linspace(min(x_c), max(x_c), 1000)
  yic = np.linspace(min(y_c), max(y_c), 1000)
  zic = griddata((x_c, y_c), z_c, (xic[None,:], yic[:,None]))
  CS = plt.contour(xic,yic,zic,15,linewidths=0.5,colors='k')
  CS = plt.contourf(xic,yic,zic,15,cmap=plt.cm.summer)
  plt.colorbar() # draw colorbar
  #
  #plt.scatter(x_c, y_c, marker='o', s=5, zorder=10)
  plt.axis('equal')
  plt.savefig('foo.pdf', bbox_inches='tight')
  plt.show()
  </code></pre>
  
  <p>and the output looks like</p>
  
  <p><a href="https://i.stack.imgur.com/Hrv5J.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Hrv5J.png" alt="Plot of the contour plot overlaid with the original irregular points, the contour plot exceeds the edges of the original points due to the size of the convex hull"></a></p>
  
  <p>The problem is that griddata uses a convex hull and this convex hull exceeds the edges of the irregular data. Is there any way to set the values of the griddata points which are outside the edges of the boundary of the original points to zero?</p>
  
  <p><strong>Edit</strong></p>
  
  <p>In the end I threw in the towel and reverted back to Matlab. I'll have to export the data to pgfplots to get a nice plot. The code I came up with was </p>
  
  <pre><code>x = strain.x;
  y = strain.y;
  z = strain.eps;
  % Get the alpha shape (couldn't do this in python easily)
  shp = alphaShape(x,y,.001);
  % Get the boundary nodes
  [bi, xy] = boundaryFacets(shp);
  no_grid = 500;
  xb=xy(:,1);
  yb=xy(:,2);
  [X,Y] = ndgrid(linspace(min(x),max(x),no_grid),linspace(min(y),max(y),no_grid));
  Z = griddata(x,y,z,X,Y,'v4');
  
  % Got through the regular grid and set the values which are outside the boundary of the original domain to Nans
  for j = 1:no_grid
      [in,on] = inpolygon(X(:,j),Y(:,j),xb,yb);
      Z(~in,j) = NaN;
  end
  
  contourf(X,Y,Z,10),axis equal
  colorbar
  hold on
  plot(xb,yb)
  axis equal
  hold off
  </code></pre>
  
  <p>Here is the resulting image.<a href="https://i.stack.imgur.com/u53lD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/u53lD.png" alt="enter image description here"></a></p>
  
  <p>If someone can do something similar in Python I'll happily accept the answer.</p>