title: Heatmap on x-y plane with z as weighting
body: <p>I've gone through tens of answers regarding heatmaps on this forum but I am still running into problems, so I thought I'd ask myself. Bare in mind that until a month ago I had no idea what Python was.</p>
  
  <p>So, I have a large file of data in three columns. The first two are standard x-y coordinates. For each point, there is a third variable, z, that I want to use as weighting to build some sort of heatmap.</p>
  
  <p>I have seen several methods, e.g. using meshgrid or changing array size, but what I think the problem is is that my array is not regular or rectangular. It's just a mess of random points in the x-y plane, not evenly spaced with each other, each with a z value.</p>
  
  <p>Here is just a small snippet of the data I have in my spreadsheet:</p>
  
  <pre><code>x y z
  392 616 0.5
  416 614 1
  497 603 3
  533 598 3.5
  383 589 0.5
  574 574 4
  ...
  </code></pre>
  
  <p>I tried several methods, e.g. reshaping the arrays, but I always get some kind of error. How can I plot this data as a heatmap with the weighting of each point given by z? Thank you.</p>
  
  <p>I'm aware that, since the data points are not regularly spaced out, there might be gaps where the heatmap would be zero, but I can sort those out later by extrapolating their weighting via a method I figured out, so that wouldn't be a problem.</p>
  
  <p>The closest I got to getting the graph I'm looking for is using this code:</p>
  
  <pre><code>plt.hist2d(x, y, bins=8, weights=z, cmap="Greys")
  plt.colorbar()
  </code></pre>
  
  <p>However, the problem with this is that, if there is more than one point in a given "bin", it computes the "aggregate" weighting -- e.g. if in a particular bin there are two data points with weightings of 1 and 2.5, respectively, the bin will be coloured as if its weighting was 1+2.5=3.5. Is there any way I can get it to display the colour corresponding to the weighting of the data point closest to the bin center? </p>
  
  <p>e.g. if the data point with weighting 2.5 was really close to the bin center while the one with weighting 1 was along one of the bin's edges, is there a way I can get the bin to have weighting 2.5?</p>
  
  <p>Thank you and sorry for disturbing.</p>