Matplotlib interpolating / plotting unstructured data
<p>usually, Im working with image data which is in arrays the shape of m x n. Displaying is easy, right?</p>

<pre><code>import numpy as np
import matplotlib.pyplot as plt

#sample data
m,n = 10,20
sample = np.random.random((m,n))
plt.imshow(sample)
</code></pre>

<p>from a different algorith, Im now getting data which has x,y,data from a different shape as m,n but in the same range. Let me generate some sample to better explain.</p>

<pre><code>k = 300
x = np.random.random((k,)) * m
y = np.random.random((k,)) * n
data = np.random.random((k,))
</code></pre>

<p>I want to do two things im not getting my head around. </p>

<p>1) Plotting the data in an imshow style</p>

<p>2) For better comparability, interpolate the data on a rectangular m,n grid, that is genrating an array of shape m,n with values from data (like "nearest"). </p>

<p>Im actuallte using scipy.ndimage.map_coordinates to do the thing the way around but cant get it working inversed...</p>

<p>Cheers</p>

<p>Below is slightly changed example of <a href="http://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.interpolate.griddata.html#scipy.interpolate.griddata" rel="nofollow">scipy.interpolate.griddata</a> usage:</p>

<pre><code>import numpy as np
from scipy.interpolate import griddata
import matplotlib.pyplot as plt

k = 300
m, n = 10, 20

x = np.random.random((k,)) * m
y = np.random.random((k,)) * n
data = np.random.random((k,))

grid_x, grid_y = np.mgrid[0:m+1, 0:n+1]

grid_z0 = griddata((x, y), data, (grid_x, grid_y), method='nearest')
grid_z1 = griddata((x, y), data, (grid_x, grid_y), method='linear')
grid_z2 = griddata((x, y), data, (grid_x, grid_y), method='cubic')

plt.subplot(131)
plt.imshow(grid_z0.T)
plt.title('Nearest')

plt.subplot(132)
plt.imshow(grid_z1.T)
plt.title('Linear')

plt.subplot(133)
plt.imshow(grid_z2.T)
plt.title('Cubic')

plt.gcf().set_size_inches(6, 6)
plt.show()
</code></pre>

python matplotlib