The weights parameter expects an array with the same length as x and y . np.histogram2d . It will not translate a constant value, so even though the mass is the same for every np.histogram2d call, you should still use something like
weights=np.ones_like(x)*mass
Now one problem that you may encounter if you use bin=nbin is that the edges of the buffer, xedges , yedges may vary depending on the x and y values ββthat you pass to np.histogram2d . If you naively add heat maps together, the end result will accumulate particle density in the wrong places.
So, if you want to call np.histogram2d more than once and add partial heat maps together, you must first determine where you want the edges of the hopper.
For instance:
import numpy as np import itertools as IT import matplotlib.pyplot as plt N = 50 nbin = 10 xs = [np.array([i,i,i+1,i+1]) for i in range(N)] ys = [np.array([i,i+1,i,i+1]) for i in range(N)] masses = np.arange(N) heatmap = 0 xedges = np.linspace(0, N, nbin) yedges = np.linspace(0, N, nbin) for x, y, mass in IT.izip(xs, ys, masses): hist, xedges, yedges = np.histogram2d( x, y, bins=[xedges, yedges], weights=np.ones_like(x)*mass) heatmap += hist extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]] heatmap = np.flipud(np.rot90(heatmap)) fig, ax = plt.subplots() ax.imshow(heatmap, extent=extent, interpolation='nearest') plt.show()
gives
