Beatriz Santos
Beatriz Santos

Reputation: 175

Giving nodes in an ox graph a color based on an attribute

I have an ox graph, called G_map, with a geo data frame for nodes associated, as the following code.

G_nodes = ox.graph_to_gdfs(G_map,edges=False)

One of the G_nodes attributes is called 'osmid' and it's the ID of the node in open street map, so every single node has a different 'osmid'. My goal here is to plot the G_map graph giving certain nodes different colors, depending on the node ID, e.g., 'osmid'. For instance, if a node has a certain ID i would like for it to be red. I think I will have to use the osmnx function but I don't know how to do it exactly,

osmnx.plot.get_node_colors_by_attr()

I have also tried this:

node_c = ['b' if ('osmid'==25620516 or 'osmid'==25620570) else 'r' for idx in G_nodes(keys=True)]
fig, ax = ox.plot_graph(G_map, node_color=node_c, node_edgecolor='w', node_size=30, node_zorder=3, edge_color=ec, edge_linewidth=3)

But i get an error saying:

TypeError: 'GeoDataFrame' object is not callable

If anyone could help me, Thank you!

Upvotes: 0

Views: 467

Answers (1)

Sparky05
Sparky05

Reputation: 4892

You should be able to simply iterate over the graph and use the attributes of the nodes directly, e.g.:

['b' if (data['osmid']==25620516 or data['osmid']==25620570) else 'r' for node, data in G_map.nodes(data=True)]

Upvotes: 1

Related Questions