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

标签 matplotlib networkx osmnx

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

代码如下:

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

剧情如下:

enter image description here

最佳答案

bbox(边界框)作为关键字参数传递给 plot_graph_routes , 它将传递给 plot_graph通过文档中描述的 plot_graph_route。文档解释说,您可以因此将绘图约束到边界框,这在 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

关于matplotlib - 如何将 'zoom' 到 osmnx 图中的特定区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62819355/

相关文章:

python - FigureCanvasTkAgg 的声明导致内存泄漏

python - 如何在 Python 中使用 OSMnx 填充水体

python - 如何在osmnx图中创建颜色栏

python - 如何在networkx中绘制带有社区结构的小图

python - 删除几乎平行的 NetworkX 最短路径

python - matplotlib 使日期的轴刻度标签变为粗体

python - 在曲面图上画线

python - 使用 CSV 数据 python 创建条形图

python networkx - 通过着色标记边缘以绘制图形

python - 根据 graphviz_layout 中的权重设置边长