If I understand you correctly: you have a set of points, each of which has an x-coordinate, a y-coordinate and a weight associated with it.
The answers in the question you linked to already describe pretty much exactly what you want to do. The only difference is that you can use the weights=
argument to get the weighted count in each bin.
For example, you could use
wcounts, xedges, yedges = np.histogram2d(x, y, weights=w)
to get your weighted histogram, then do
extent = xedges[0], xedges[-1], yedges[0], yedges[-1]
imshow(wcounts, extent=extent, alpha=0.5)
to display it.
I don't know what you mean by overlaying it on a 'map', but you can use the alpha=
argument to imshow()
to make the image semi-transparent (or you could just draw your 'map' on top of the image).
Likewise, you could do
hexbin(x, y, C=w, alpha=0.5)
to draw a weighted hexagonal binning plot, in this case using the C=
argument to specify the weights, and again using alpha=
to control the transparency of the plot.
Edit
Ok, so you want to compute the histogram over a specified grid of bin locations. Supposing your x-coordinates are positions between 0m and 100m, your y-coordinates are between 0m and 75m, and you want each bin to be 1m by 1m. You can pass a tuple of arrays specifying the bin edges in x and y to np.histogram2d()
:
# remember that for n bins there are n+1 bin edges
x_edges = np.linspace(0, 100, 101)
y_edges = np.linspace(0, 75, 76)
wcounts = np.histogram2d(x, y, weights=w, bins=(x_edges, y_edges))[0]
Now wcounts
is a (100, 75)
array, with each element representing the weighted count in a 1m by 1m bin.