'Coloring osm graph edges in python

I'm trying to make a map where a certain route, e.g. the edges included in the route, is colored. For now, I have the following code:

node_route=[1223724841, 1688813022, 2464466873, 1688813022, 1688813022, 1688813022, 1688813022]
ec = ['w' for u, v, k in G_map.edges(keys=True)] 
for i in range(len(node_route)-1):
    ec = ['r' for u, v, k in G_map.edges(keys=True) if (u==node_route[i] and v==node_route[i+1])]
    
fig, ax = ox.plot_graph(G_map, node_color='r', node_edgecolor='k', node_size=40, node_zorder=3, edge_color=ec, edge_linewidth=1)  

The list node_rote is the ordered route i would like to color. I start by assigning 'w' to every edge in my graph (G_map). Then i check every pair in my node_route list (index i and i+1) and if the pair is equal to 'u' and 'v' of the graph edges it will be colored with 'r'. The problem is this code returns an empty list.

Thanks for the help!



Solution 1:[1]

For starters, you are re-assigning ec each time through the loop, throwing away the earlier value.

If I understand correctly what you are trying to do, you might be better off building a dictionary that maps node id:s to their position in node_route. Then loop over G_map.edges(...), looking up the positions of the u and v nodes in node_route using your dictionary, and appending 'r' or 'w' to ec depending on whether pos[u]+1 == pos[v] or not.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ture PÄlsson