Search code examples
networkxfoliumosmnx

Networkx OSMNX and Folium plotting different coloured edges


I have been trying to overlay different osmnx plots onto folium but I have been unable to. I want to achieve the same thing as shown in the image below with a simple osmnx plot but instead on folium with the red edges being red and the other edges being a different colour.

fig, ax = ox.plot_graph_routes(G,max_response_edges, bgcolor='k', node_size=30, node_color='#999999', node_edgecolor='none', node_zorder=2,
                        edge_color='#555555', edge_linewidth=1.5, edge_alpha=1,figsize = (20,20))

enter image description here

I have tried to use the following code:

H = G.copy()
H1 = G.copy()
H.remove_edges_from(G.edges - set(map(lambda x: tuple(x)+(0,),max_response_edges)))
m = ox.folium.plot_graph_folium(H,tiles='openstreetmap',popup_attribute='name',opacity = 1,color = 'red',weight= 10)
H1.remove_edges_from(set(map(lambda x: tuple(x)+(0,),max_response_edges)))
n = ox.folium.plot_graph_folium(H1,tiles='openstreetmap',popup_attribute='name',opacity = 1,color = 'blue',weight= 10)
n.add_to(m)
m

enter image description here

I tried making use of m.add_child() or m.add_to() but none have proved useful. A similar stack overflow question was posted here however this did not work. Can folium overlays be done?


Solution

  • Got it working through the following code

    H = G.copy()
    H1 = G.copy()
    H.remove_edges_from(G.edges - set(map(lambda x: tuple(x)+(0,),max_response_edges)))
    m = ox.folium.plot_graph_folium(H,tiles='openstreetmap',popup_attribute='name',opacity = 1,color = 'red',weight= 10)
    H1.remove_edges_from(set(map(lambda x: tuple(x)+(0,),max_response_edges)))
    m = ox.folium.plot_graph_folium(H1, graph_map = m,tiles='openstreetmap',popup_attribute='name',opacity = 1,color = 'blue',weight= 10)
    m
    

    turns out I was missing the graph_map=m parameter