0

I found that the hist2d generates the density heatmap extremely fast.

When I want to reproduce the function and then apply to 3d to generate 3d density heatmap, it seems extremely slow (which should be caused by the slow python 'for' loop):

xi, yi = np.mgrid[-30:30:200*1j, -30:30:200*1j]

step = 60/200

count_grid = np.full_like(yi, np.nan)
for idx, value in np.ndenumerate(x):
    mask_grid   = (x_mask>xi[idx]-0.5*step) & (x_mask<xi[idx]+0.5*step) & (y_mask>yi[idx]-0.5*step) & (y_mask<yi[idx]+0.5*step)
    count_grid[idx] = len(x_mask[mask_grid])

So I would like to find hist2d's source code, which provided by matplotlib is a boilerplate one, I do not know where to find the useful one.

Anyone can help?

Hangci Du
  • 25
  • 1
  • 5
  • 2
    The `for` loop you introduced, doesn't allow making use of the vectorization which makes numpy fast. Note that matplotlib leans on [`np.histogram2d`](https://numpy.org/doc/stable/reference/generated/numpy.histogram2d.html), which you could also use for a 3D extension. However, 200x200 might be too much for matplotlib to handle. – JohanC Dec 25 '21 at 16:13
  • See also [Create 3D histogram of 2D data](https://matplotlib.org/stable/gallery/mplot3d/hist3d.html) from matplotlib tutorials. – JohanC Dec 25 '21 at 17:11

0 Answers0