-1

I have a map, on top of which I wish to overlay a color weight map. The weight map has m x n 1m x 1m boxes. I have many points, and each point carries a weight. The weight of a box is calculated by summing up the weights of all the points that fall into that box.

Now according to their wights, a certain color is filled for each box according to its weight.

The desired outcome is similar to the one shown here, but

  1. The map and the weight map have to be overlayed nicely in my case.
  2. Instead of frequency, the weights that have been calculated should be used to assign the color.

How may I do this?

Community
  • 1
  • 1
Sibbs Gambling
  • 19,274
  • 42
  • 103
  • 174

1 Answers1

1

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.

ali_m
  • 71,714
  • 23
  • 223
  • 298
  • *or you could just draw your 'map' on top of the image* great advice and life-saving post! My only concern is the scale difference between the histogram and the map. See, the map is in meter scale and spans for 200m^2 or so. And then what about the scale of the histogram. I am really confused. **Each cell of the histogram should correspond to a 1m*1m square on the map** – Sibbs Gambling Oct 11 '13 at 13:50
  • you can do this by explicitly specifying the bin edges for `np.histogram2d()`. see my edit. – ali_m Oct 11 '13 at 14:44
  • Thank you so much for the update. Another very radical difference in my case is that I expect the weights of the points that fall in the same square to be summed up. i.e., since my square is 1m*1m, so point (0.75, 0.5) and (0.4, 0.1) should fall into the same cell, and their weights are to be summed up to form **the weight of the cell**. Does your update automatically do this? Thanks a lot! – Sibbs Gambling Oct 11 '13 at 15:19
  • Yes - literally the only difference between your case and the question you linked to is that the value in each bin is the _sum of the weights_ of the points that fall in that bin, rather than just the _number of points_ that fall in that bin. Try it and check for yourself! – ali_m Oct 11 '13 at 16:13
  • Awesome. Everything works perfectly except my x and y values are interchanged... http://pastie.org/private/dgrizx3iwqplwk1ypv7ya – Sibbs Gambling Oct 11 '13 at 16:16
  • Also, how may I save the histogram? I notice savefig(PROJECT_PATH + '\\data\\filename.svg') fails – Sibbs Gambling Oct 11 '13 at 16:27
  • 1
    "my x and y values are interchanged" - that's because `imshow()` treats the first dimension of the array as rows, i.e. y-values. Just transpose `wcounts`. "I notice savefig(PROJECT_PATH + '\\data\\filename.svg')" - that's probably not a valid filepath. Try just saving it in your current working directory. – ali_m Oct 11 '13 at 16:45