3

I have a osmnx plot that has two routes, but the map is quite vast therefore I cannot see the routes properly. Is there a fast way to limit my plot, to sort of 'zoom in', using for example bbox? I know I could search for districts instead of a whole city, I was just curious to know if there was a fastest way to 'zoom' in the plot.

Here is the code:

import osmnx as ox
import igraph as ig
import matplotlib.pyplot as plt
import pandas as pd
import networkx as nx
import numpy as np
import matplotlib as mpl
import random as rd
from IPython.display import clear_output
import matplotlib.cm as cm
ox.config(log_console=True, use_cache=True)

%%time
city = 'Portugal, Lisbon'
G = ox.graph_from_place(city, network_type='drive', simplify=True)

G_nx = nx.relabel.convert_node_labels_to_integers(G)
weight = 'length'
nodes, edges = ox.graph_to_gdfs(G_nx, nodes=True, edges=True)
origin = [8371, 5983, 6301, 9086]

orig = origin[1]
dest = 9590
route_1 = nx.shortest_path(G_nx, orig, dest, weight='length')
route_2 = nx.shortest_path(G_nx, dest, orig, weight='length')

fig, ax = ox.plot_graph_routes(G_nx, routes=[route_1, route_2], route_colors=['r', 'y'],
                               route_linewidth=6, node_size=0, figsize=(20,20))

Here is the plot:

enter image description here

DPM
  • 845
  • 7
  • 33
  • 1
    `ax.set_xlim`, `ax.set_ylim` – Paul H Jul 09 '20 at 16:27
  • That will probably work, but ax.axis('on') does not show me the axis so I don't know the limits I should implement – DPM Jul 09 '20 at 16:39
  • 1
    Probably their are getters, either `ax.get_xlim`, `ax.xlim`, or `plt.xlim` (I always forget which is possible at `plt` or axis level). Alternatively, you may want to take a look at [`folium`](https://python-visualization.github.io/folium/quickstart.html). – Sparky05 Jul 09 '20 at 16:42
  • 1
    if you're using an interactive backend, there will be an box zoom widget. Otherwise I'd take a direct look at the data to see where things are – Paul H Jul 09 '20 at 16:48
  • 1
    The image doesn't change when I use ax.set_xlim and ax.set_ylim – DPM Jul 09 '20 at 17:11

1 Answers1

3

Pass a bbox (bounding box) as a keyword arg to plot_graph_routes, which will pass it along to plot_graph via plot_graph_route as described in the docs. The documentation explains that you can thus constrain a plot to a bounding box, and this is demonstrated in the example notebooks.

import networkx as nx
import osmnx as ox
ox.config(use_cache=True, log_console=True)

# get a graph
G = ox.graph_from_place('Piedmont, CA, USA', network_type='drive')

# get 2 shortest paths
r1 = nx.shortest_path(G, list(G)[0], list(G)[-1], weight='length')
r2 = nx.shortest_path(G, list(G)[10], list(G)[-10], weight='length')

# constrain plot to a bounding box
pt = ox.graph_to_gdfs(G, edges=False).unary_union.centroid
bbox = ox.utils_geo.bbox_from_point((pt.y, pt.x), dist=500)
fig, ax = ox.plot_graph_routes(G, [r1, r2], ['y', 'r'], bbox=bbox)

OSMnx plotting multiple routes and zooming in to a bounding box

gboeing
  • 5,691
  • 2
  • 15
  • 41