How does osmnx.plot_graph determine which edges get which colors?

Viewed 52

I've followed this tutorial: https://towardsdatascience.com/creating-beautiful-maps-with-python-6e1aae54c55c and the one this above was derived from.

They pass a list of edge colors to the plot_graph function like so:

fig, ax = ox.plot_graph(gdf, node_size=0, bbox = (north, south, east, west),figsize=(height, width),
                    dpi = 96,bgcolor = bgcolor,
                    save = False, edge_color=roadColors,
                    edge_linewidth=roadWidths, edge_alpha=1)

I don't think they're assigned the way that the tutorial indicates.

On the github I found get_edge_colors_by_attr which seems to take attributes into account.

How are the colors assigned?

Specifically I am asking because I'd like to plot "highways" in different colors based on their openstreetmap tag.

1 Answers

How does osmnx.plot_graph determine which edges get which colors?

You can see how it does it here. Essentially, it either applies a single color to all edges or, if you passed it a list of colors, it assigns the first color in the list to the first edge in the graph, the second to the second, the third to the third, and so on.

Specifically I am asking because I'd like to plot "highways" in different colors based on their openstreetmap tag.

You can create a list of colors based on the edges' highway attribute values:

import osmnx as ox
G = ox.graph_from_place('Piedmont, California, USA', network_type='drive')

# assign colors to edges based on "highway" value
hwy_color = {'residential': 'gray', 
             'secondary': 'r',
             'tertiary': 'y',
             'tertiary_link': 'b',
             'unclassified': 'm'}
edges = ox.graph_to_gdfs(G, nodes=False)['highway']
ec = edges.replace(hwy_color)

# plot graph using these colors
fig, ax = ox.plot_graph(G, edge_color=ec)

Also, you mentioned get_edge_colors_by_attr but note that per the docs the attribute must be numeric.

Related