How to create a heat map matrix and generate regio

2019-09-11 08:16发布

问题:

Given a set of points (x, y, 'heat'),

In [15]: df.head()
Out[15]: 
          x         y      heat
0  0.660055  0.395942  2.368304
1  0.126268  0.187978  6.760261
2  0.174857  0.637188  1.025078
3  0.460085  0.759171  2.635334
4  0.689242  0.173868  4.845778

How to generate a heat map matrix and delimit heat regions (hard)?

in such a way that, given a point, it is possible to get all points within the same region.

PS: From Generate a heatmap in MatPlotLib using a scatter data set, I know how to generate graphs of regions, but not how to generate the region 'matrix' (so that given a property, it says in which region it is).

回答1:

I guess it depend how you did the heatmap but assuming you used the first example from the post you linked:

import numpy as np
import numpy.random
import matplotlib.pyplot as plt

# Generate some test data
x = np.random.randn(8873)
y = np.random.randn(8873)

heatmap, xedges, yedges = np.histogram2d(x, y, bins=50)
extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]

plt.clf()
plt.imshow(heatmap, extent=extent)
plt.show()

So you now if you have a request about a point with coords (a,b) you need to find the position of the nearest value to a in xedges (lets call it a_heatmap), the position of the nearest value of b in yedges (b_heatmap), then look for the returned value by :

heatmap[a_heatmap, b_heatmap]