简体   繁体   中英

Heat Map Python

I am trying to plot a heat map. The data that I use has Geo coordinates and time in seconds. The nature of data is edges encounter each other at certain geo location and spend some time connected. Considering the geo location and the time those edges are connected to each other I need to plot a heat map.

The edges are connected to each other and they happen to be connected for certain amount of time in seconds and at that time they are in certain geo location. I need to plot a heat map to identify the crowd density and the time spent in that location.

col[0] and col[1] are edges col[2] time connected col[3] to col[7] are the geo coordinates.

0006251fda59 00904bc9dd3c 11.0 821141 439384 821141 439384
0004233019fe 0004233cd875 23.0 818612 439965 818612 439965
0004233019fe 000423aa3632 1572.0 818612 439965 818612 439965
0004233019fe 000423dfbc68 200.0 818612 439965 818612 439965
0004233019fe 000423fbb938 648.0 818612 439965 818612 439965
0004233019fe 000423fcb610 1999.0 818612 439965 818612 439965
00022d1aa531 00028a2d154f 10.0 821007 438860 821007 438860
00022d428ff1 00028a2d154f 10.0 821007 438860 821007 438860

Any suggestion on how to begin with is appreciated.

You first need to discretize your coordinates. As I'm guessing for your sample data, col[0] and col[1] represent nodes (edge between them two), so you can use the node name to discretize the coordinates. To do that, assign a number to each node.

To extract all nodes:

set1 = set([col[0] for col in data])
set2 = set([col[1] for col in data])
nodes = list(set1.union(set2))

Where data is your data matrix. Discretize them:

mapping = {}
for i, node in enumerate(nodes):
    mapping[node] = i

Create your heat map container:

import numpy as np
n = len(nodes)
heatmap = np.zeros((n,n))

Populate heatmap:

for col in data:
    coord1 = mapping[col[0]]
    coord2 = mapping[col[1]]
    heatmap[[coord1, coord2], [coord2, coord1]] += col[2]

And now you can just plot the imshow or hist2d as @greschd suggested.

imshow(heatmap, 'jet', interpolation=None)

Or build a more complex heatmap as the link by @en_Knight explains.

NOTE: I did assume that your heat map is npoints x npoints and each point is represented by either its (x,y) coordinates or its node identifier, thus, only works if as I guessed from your examples, the same node identifier (hash) always has same coordinates.

If I understand you right you want a heatmap looking something like the one in this post . So because your data is just points the first thing you have to do is subdivide your region into squares (grid). Then simply add up the timings for all the points lying in a given square, which gives you a 2D list.

From that, you can generate the heatmap either with imshow or hist2d from matplotlib .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM