title: Interpolation over 2d unstructured grid data
body: <p>I have some <a href="https://vdegner.de/owncloud/index.php/s/kggfsoPZ4gFWUPN" rel="nofollow noreferrer">data</a> (x,y,z) lying on an unstructured grid and I would like to interpolate the data for visualization purposes.</p>
  
  <p>I have already tried <code>scipy.interpolate.griddata</code>, the interpolation assumes everywhere the same value. After that I tried <code>scipy.interpolate.Rbf</code>, but this gets me a memory error (see code below).  </p>
  
  <p>Is there another method or other options which improve the result? </p>
  
  <p><strong>Result--></strong> </p>
  
  <p><img src="https://i.stack.imgur.com/uvYgN.png" alt="result"></p>
  
  <p><strong>My code</strong></p>
  
  <pre><code>import numpy as np
  import matplotlib.pyplot as plt
  from scipy.interpolate import griddata, Rbf
  
  x, y, z = np.loadtxt('stackoverflow_example-data')
  # griddata
  points = np.reshape(np.array([x, y]),(z.size, 2))
  grid_x, grid_y = np.mgrid[x.min():x.max():1000j,y.min():y.max():1000j]
  counts_I_grid_1 = griddata(points, z, (grid_x, grid_y), method='nearest')
  counts_I_grid_2 = griddata(points, z, (grid_x, grid_y), method='linear', fill_value=0)
  counts_I_grid_3 = griddata(points, z, (grid_x, grid_y), method='cubic', fill_value=0) 
  
  # Rbf -- fails due to memory error
  #rbf = Rbf(x,y,z)
  #counts_I_Rbf = rbf(grid_x,grid_y)
  </code></pre>
  
  <blockquote>
    <p>Traceback (most recent call last):
          File "/path/code.py", line 14, in 
            rbf = Rbf(x,y,z)
          File "/[...]/python3.4/site-packages/scipy/interpolate/rbf.py", line 198, in <strong>init</strong>
            r = self._call_norm(self.xi, self.xi)
          File "/[...]/python3.4/site-packages/scipy/interpolate/rbf.py", line 222, in _call_norm
            return self.norm(x1, x2)
          File "/[...]/python3.4/site-packages/scipy/interpolate/rbf.py", line 114, in _euclidean_norm
            return sqrt(((x1 - x2)**2).sum(axis=0))
        MemoryError</p>
  </blockquote>
  
  <pre><code># plot the result
  fig = plt.figure()
  
  ax1 = plt.subplot(2,2,1)
  plt.title('Data')
  plt.gca().set_aspect((x.max() - x.min()) / (y.max() - y.min()))
  plt.scatter(x, y, c=z, s=2, edgecolor='', marker=',')
  plt.colorbar(ax=ax1)
  plt.xlim(x.min(), x.max())
  plt.ylim(y.min(), y.max())
  plt.xticks([20.7,20.9,21.1,21.3])
  plt.ticklabel_format(useOffset=False)
  
  ax2 = plt.subplot(2,2,2)
  plt.title('nearest')
  plt.imshow(counts_I_grid_1.T, origin='lower',
             extent=(x.min(), x.max(), y.min(), y.max()),
             aspect=(x.max() - x.min()) / (y.max() - y.min()),
             vmin=0,vmax=36)
  plt.colorbar(ax=ax2)
  plt.xticks([20.7,20.9,21.1,21.3])
  plt.ticklabel_format(useOffset=False)
  
  ax2 = plt.subplot(2,2,3)
  plt.title('linear')
  plt.imshow(counts_I_grid_2.T, origin='lower',
             extent=(x.min(), x.max(), y.min(), y.max()),
             aspect=(x.max() - x.min()) / (y.max() - y.min()),
             vmin=0,vmax=36)
  plt.colorbar(ax=ax2)
  plt.xticks([20.7,20.9,21.1,21.3])
  plt.ticklabel_format(useOffset=False)
  
  ax2 = plt.subplot(2,2,4)
  plt.title('cubic')
  plt.imshow(counts_I_grid_3.T, origin='lower',
             extent=(x.min(), x.max(), y.min(), y.max()),
             aspect=(x.max() - x.min()) / (y.max() - y.min()),
             vmin=0,vmax=36)
  plt.colorbar(ax=ax2)
  plt.xticks([20.7,20.9,21.1,21.3])
  plt.ticklabel_format(useOffset=False)
  plt.tight_layout()
  plt.show()
  </code></pre>