如何 'zoom' 到 osmnx 图中的特定区域

How to 'zoom' to a specific area in an osmnx plot

我有一个有两条路线的 osmnx 图,但地图非常大,因此我无法正确看到路线。有没有一种快速的方法来限制我的情节,例如 'zoom in',使用 bbox?我知道我可以搜索地区而不是整个城市,我只是想知道是否有最快的方式到达 'zoom' 地块。

代码如下:

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))

剧情如下:

bbox(边界框)作为关键字参数传递给 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)