title: Numpy meshgrid from unordered x, y, z data
body: <p>I have x, y and z data which looks like the following (sample data):</p>
  
  <pre><code>x_data = [2, 2, 2, 3, 3, 3, 1, 1, 1, 4, 4, 4]
  y_data = [16, 64, 32, 64, 32, 16, 16, 32, 64, 32, 16, 64]
  z_data = [64, 31, 29, 78, 72, 63, 93, 40, 54, 35, 44, 3]
  </code></pre>
  
  <p>Since I can map every z value to an unique x/y data pair, I would like to plot it as a filled countour plot in matplotlib.    <code>contourf</code>    requires x and y to be either meshgrids or to be the size of the corresponding dimension of z.</p>
  
  <p>At the moment the code I use to prepare the data is the following:</p>
  
  <pre><code>mesh_x, mesh_y = np.meshgrid(np.unique(x_data), np.unique(y_data))
  mesh_z = np.zeros(mesh_x.shape)
  
  for x, y, z in zip(x_data, y_data, z_data):
      mesh_z[np.bitwise_and(y == mesh_y, x == mesh_x)] = z
  </code></pre>
  
  <p>Since <code>np.unique</code> reorders the values, I can not use <code>np.reshape(z_data, mesh_x.shape)</code> to create the needed data structure. </p>
  
  <p>Although this solution works, I feel like there must be a better way to archieve this.</p>