Ferrix
Ferrix

Reputation: 59

Assign a color to a specific edge in osmnx graph based on a given u,v node values

I am plotting a osmnx graph: while I can control nodes color, I am unable to control the edge color.

My goal is to assign color blue only to the edge defined by u_node = 4515988732 and v_node=2021402216. I have tried:

for u,v,k in G.edges(keys=True, data=False):
    if (u==4515988732 and v==2021402216):
       ev='b'

but all the edges are plotted in blue when I plot them with:

fig, ax = ox.plot_graph(G, fig_height=7, node_color=nc, 
node_size=10, node_alpha=0.8, node_zorder=2,
edge_color=ev, edge_linewidth=1)

I also tried:

ev=[(u,v,'b') for u,v,k in G.edges(keys=True, data=False) if 
(u==4515988732 and v==2021402216)]

but in this case I get a Invalid RGBA argument error. I spent a lot of time but I am new to osmnx and I am unable to find the correct syntax to achieve my goal: where do I go wrong here?

Upvotes: 1

Views: 3412

Answers (1)

gboeing
gboeing

Reputation: 6442

The OSMnx examples demonstrate how to color edges according to some trait.

ec = ['b' if (u==4515988732 and v==2021402216) else 'r' for u, v, k in G.edges(keys=True)]
fig, ax = ox.plot_graph(G, node_color='w', node_edgecolor='k', node_size=30, 
                           node_zorder=3, edge_color=ec, edge_linewidth=3)

Upvotes: 3

Related Questions