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

 ̄綄美尐妖づ 提交于 2020-12-06 02:59:29

问题


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:


回答1:


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)



来源:https://stackoverflow.com/questions/62819355/how-to-zoom-to-a-specific-area-in-an-osmnx-plot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!