Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create heatmap in Python using OSMnx and a list of values corresponding to the graph's nodes?

I've built a simulation model of pedestrians walking on a network using OSMnx and one of the simulation's outputs is a list "Visits" that is corresponding to the nodes in NodesList = list(Graph.nodes). How can I create an heatmap using those lists and OSMnx?

For example:

NodesList[:5]
Output: [1214630921, 5513510924, 5513510925, 5513510926, 5243527186]

Visits[:5]
Output: [1139, 1143, 1175, 1200, 1226]

P.S. the type of heatmap is not important (Nodes size, nodes color, etc.)

like image 277
Guy Avatar asked Dec 03 '25 16:12

Guy


1 Answers

Since you specified the type of heatmap is not important, I have come up with the following solution.

import osmnx as ox
address_name='Melbourne'

#Import graph
G=ox.graph_from_address(address_name, distance=300)

#Make geodataframes from graph data
nodes, edges = ox.graph_to_gdfs(G, nodes=True, edges=True)

import numpy as np
#Create a new column in the nodes geodataframe with number of visits
#I have filled it up with random integers
nodes['visits'] = np.random.randint(0,1000, size=len(nodes))

#Now make the same graph, but this time from the geodataframes
#This will help retain the 'visits' columns
G = ox.save_load.gdfs_to_graph(nodes, edges)

#Then plot a graph where node size and node color are related to the number of visits
nc = ox.plot.get_node_colors_by_attr(G,'visits',num_bins = 5)
ox.plot_graph(G,fig_height=8,fig_width=8,node_size=nodes['visits'], node_color=nc)

enter image description here

like image 89
Debjit Bhowmick Avatar answered Dec 06 '25 15:12

Debjit Bhowmick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!