I am using OSMNx to plot shortest path routes and I cannot figure out a way to plot multiple routes on the same map. Here's my code so far,
route_list = []
for i in range(len(pick_drop_outliers_ratio)):
coords = pick_drop_outliers_ratio["Pickup_Points"][i]
count = pick_drop_outliers_ratio["Count"][i]
print("i: ", i, " count: ", count)
if(count>9):
coords = literal_eval(coords)
pickup_lat = (coords[0][0])
pickup_lon = (coords[0][1])
dropoff_lat = (coords[1][0])
dropoff_lon = (coords[1][1])
orig_node = ox.get_nearest_node(G, (pickup_lat, pickup_lon))
dest_node = ox.get_nearest_node(G, (dropoff_lat, dropoff_lon))
route = nx.shortest_path(G, orig_node, dest_node, weight='length')
route_list.append(route)
fig, ax = ox.plot_graph_route(G, route_list, node_size=0)
I wish to plot every route in route_list on the same map in different colors for each route. Since there is no built in function in OSMNx, is there a way to do this?
I found the solution is "plotting the graph then adding routes manually on top with matplotlib." But Could not implement.
The osmnx package represents routes as a networkx graph – so we can do graphy things with it, like finding the shortest distance between two points, aka route planning: The route can also be plotted on an interactive map. An option lets you hover on a route and display a context sensitive tooltip, in this case the name of the road:
It’s quite incredible… Pretty much a one-liner lets you pull back all the roads in a particular area, searched for by name: The osmnx package represents routes as a networkx graph – so we can do graphy things with it, like finding the shortest distance between two points, aka route planning: The route can also be plotted on an interactive map.
OSMNX has a function called graph_from_place, so you can retrieve OSM data by choosing a neighborhood, city and country and all these data come in form of a netoworkx multi digraph. nodes, edges = ox.graph_to_gdfs (graph)
4. Save street networks to disk OSMnx can save the street network to disk as a GraphML file to work with later in Gephi or networkx. Or it can save the network (such as this one, for the New York urbanized area) as ESRI shapefiles or GeoPackages to work with in any GIS:
EDIT: note that as of OSMnx 0.15.0, you can now easily plot multiple routes with their own colors easily and natively:
routes = [route1, route2, route3]
rc = ['r', 'y', 'c']
fig, ax = ox.plot_graph_routes(G, routes, route_colors=rc, route_linewidth=6, node_size=0)
For more info, see the docs or this usage example notebook.
===============================
Original answer from 2018:
It's worth noting that this functionality now exists in OSMnx as of v0.8.2. Example here of plotting multiple routes each with their own color.
import networkx as nx
import osmnx as ox
ox.config(log_console=True, use_cache=True)
G = ox.graph_from_place('Piedmont, CA, USA', network_type='drive')
# pick 4 random nodes as origins/destinations for the 2 routes
orig1 = list(G.nodes())[0]
dest1 = list(G.nodes())[-1]
orig2 = list(G.nodes())[50]
dest2 = list(G.nodes())[-50]
# calculate shortest paths for the 2 routes
route1 = nx.shortest_path(G, orig1, dest1, weight='length')
route2 = nx.shortest_path(G, orig2, dest2, weight='length')
# create route colors
rc1 = ['r'] * (len(route1) - 1)
rc2 = ['b'] * len(route2)
rc = rc1 + rc2
nc = ['r', 'r', 'b', 'b']
# plot the routes
fig, ax = ox.plot_graph_routes(G, [route1, route2], route_color=rc, orig_dest_node_color=nc, node_size=0)
It can be done as, fig, ax = ox.plot_graph_routes(G, route_list, route_color = color_list) But actually out put is not as accurate as hoped. To get perfect output use one color for one edge. But for your case this will perfectly work... `
route_list = []
for i in range(len(pick_drop_outliers_ratio)):
coords = pick_drop_outliers_ratio["Pickup_Points"][i]
count = pick_drop_outliers_ratio["Count"][i]
print("i: ", i, " count: ", count)
if(count>9):
coords = literal_eval(coords)
pickup_lat = (coords[0][0])
pickup_lon = (coords[0][1])
dropoff_lat = (coords[1][0])
dropoff_lon = (coords[1][1])
orig_node = ox.get_nearest_node(G, (pickup_lat, pickup_lon))
dest_node = ox.get_nearest_node(G, (dropoff_lat, dropoff_lon))
route = nx.shortest_path(G, orig_node, dest_node, weight='length')
route_list.append(route)
fig, ax = ox.plot_graph_routes(G, route_list, node_size=0)
`
My example
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With