【问题标题】:OSMnx: How do I plot a route onto an existing figure?OSMnx:如何在现有图形上绘制路线?
【发布时间】:2021-11-05 12:19:19
【问题描述】:

如果我已经有一个城市的图表,我可以在上面画一条或多条路线吗?到目前为止,我已经在某个边界框内创建了一个图形,然后用plt.show() 显示:

north, west = 47.45711622150064, 8.347885652066624
south, east = 47.31625428324118, 8.69120840316829

G = ox.graph.graph_from_bbox(north, south, east, west, network_type='drive', simplify=True, retain_all=False)

fig, ax = ox.plot.plot_graph(G)

plt.show()

所以现在你可以在 matplot 图中看到图形了:

figure of graph

然后我希望用户输入起点和终点坐标,并在此图上显示计算出的最短路线。但是,我不知道打开图后如何在图上绘制路线。这不起作用:

G = ox.add_edge_speeds(G)
G = ox.add_edge_travel_times(G)

start_node = ox.distance.nearest_nodes(G, 8.435432953597548, 47.39719131318801)
end_node = ox.distance.nearest_nodes(G, 8.636963408494227, 47.3699935785241)

# Calculate the shortest path
route = nx.shortest_path(G, start_node, end_node, weight='travel_time')

# Plot the route and street networks
fig, ax = ox.plot_graph_route(G, route, route_linewidth=1, show=False, close=False)

plt.show()

【问题讨论】:

标签: matplotlib networkx osmnx


【解决方案1】:

请参阅documentation。将 closeshow 参数设置为 False,以便您可以在显示之前继续更改图形。如果您在 Jupyter 笔记本中执行此操作,请确保所有代码都在一个单元格中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-06
    • 2020-03-05
    • 2021-04-17
    • 2018-12-17
    • 2021-02-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多