Klaus
Klaus

Reputation: 1731

Plotting Multiple Routes with OSMNx

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.

Upvotes: 7

Views: 10601

Answers (3)

gboeing
gboeing

Reputation: 6412

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)

osmnx plot multiple routes each with their own color

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)

OSMnx plot multiple street network routes each with own color

Upvotes: 16

MegaCreater
MegaCreater

Reputation: 101

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

Upvotes: 3

gonfy
gonfy

Reputation: 454

Take a look at OSMNX pull request #152. It hasn't been merged into OSMNX master yet, but I've used the code myself and it works well - you can simply copy it into your own script (or a utility file) fix any imports that are expected and use it pretty easily.

For multiple colors, it seems pretty easy to modify the code to accept in an array for route_color instead of a single route_color string.

Upvotes: 2

Related Questions