Reputation: 2438
I'm trying to color a route by the attributes of individual edges along that route, which used to be possible in an earlier version of osmnx with the following syntax:
# just an example graph
G = ox.graph_from_point((42.35083, -71.089473), network_type='drive', dist_type='network',
simplify=False, dist=100)
# example route
route = [61354688, 7542547660, 5674166794, 61354678]
# color for each edge (4 nodes, 3 edges)
rcs = ['r', 'b', 'r']
ox.plot_graph_route(G, route, route_color=rcs)
This now throws the following error:
ValueError: 'c' argument has 3 elements, which is not acceptable for use with 'x' with size 2, 'y' with size 2.
I'm not sure how this was implemented previously, but it seemed like it could take a list with the same number of elements as there were edges in the route and color accordingly. Is there a new way to implement this? It seems like it's possible to color the entire graph by particular attributes, but not individual routes.
Any help would be appreciated!
Upvotes: 1
Views: 965
Reputation: 6422
As you can see in the docs, the plot_graph_route
function takes a route_color
argument as a string to define the color of the plotted route. If you want to give each edge its own color, you'll want to plot the route manually on top of the graph. Here's an example, loosely adapted from the OSMnx source code:
import osmnx as ox
ox.config(use_cache=True)
# example graph and route
G = ox.graph_from_point((42.35083, -71.089473), network_type='drive', dist_type='network',
simplify=False, dist=100)
route = [61354688, 7542547660, 5674166794, 61354678]
rcs = ['r', 'b', 'r']
node_pairs = zip(route[:-1], route[1:])
# plot graph
fig, ax = ox.plot_graph(G, show=False, close=False)
# then plot colored route segments on top of it
for (u, v), rc in zip(node_pairs, rcs):
data = min(G.get_edge_data(u, v).values(), key=lambda d: d["length"])
if "geometry" in data:
x, y = data["geometry"].xy
else:
x = G.nodes[u]["x"], G.nodes[v]["x"]
y = G.nodes[u]["y"], G.nodes[v]["y"]
ax.plot(x, y, color=rc, lw=5)
Upvotes: 1