如何在osmnx图中``缩放''到特定区域 [英] How to 'zoom' to a specific area in an osmnx plot

查看:47
本文介绍了如何在osmnx图中``缩放''到特定区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个osmnx图,其中有两条路线,但是地图很大,因此我看不到正确的路线.有没有一种快速的方法来限制我的情节,例如使用 bbox 进行放大"?我知道我可以搜索地区而不是整个城市,我只是很好奇,想知道是否有最快的方法可以在地块中进行缩放".

以下是代码:

将 osmnx 导入为 ox将igraph导入为ig导入matplotlib.pyplot作为plt将熊猫作为pd导入将networkx导入为nx将numpy导入为np将Matplotlib导入为mpl将随机导入为 rd从 IPython.display 导入 clear_output将cmplotlib.cm导入为cmox.config(log_console=True, use_cache=True)%%时间城市=葡萄牙里斯本"G = ox.graph_from_place(city, network_type='drive',simple=True)G_nx = nx.relabel.convert_node_labels_to_integers(G)重量='长度'节点,边缘= ox.graph_to_gdfs(G_nx,节点=真实,边缘=真实)来源= [8371,5983,6301,9086]原点 = 原点[1]dest = 9590route_1 = nx.shortest_path(G_nx,orig,dest,weight ='length')route_2 = nx.shortest_path(G_nx,dest,orig,weight ='length')无花果,ax = ox.plot_graph_routes(G_nx,路线= [路线_1,路线_2],路线颜色= ['r','y'],route_linewidth=6, node_size=0, figsize=(20,20))

这是情节:

解决方案

bbox(边界框)作为关键字 arg 传递给

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:

解决方案

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图中``缩放''到特定区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆