gpt4 book ai didi

python - 使用 OSMNx 绘制多条路线

转载 作者:行者123 更新时间:2023-11-28 20:58:42 26 4
gpt4 key购买 nike

我正在使用 OSMNx 绘制最短路径路线,但我无法找到在同一张 map 上绘制多条路线的方法。到目前为止,这是我的代码,

route_list = []
for i in range(len(pick_drop_outliers_ratio)):
coords = pick_drop_outliers_ratio["Pickup_Points"][i]
count = pick_drop_outliers_ratio["Count"][i]
print("i: ", i, " count: ", count)
if(count>9):
coords = literal_eval(coords)
pickup_lat = (coords[0][0])
pickup_lon = (coords[0][1])
dropoff_lat = (coords[1][0])
dropoff_lon = (coords[1][1])
orig_node = ox.get_nearest_node(G, (pickup_lat, pickup_lon))
dest_node = ox.get_nearest_node(G, (dropoff_lat, dropoff_lon))
route = nx.shortest_path(G, orig_node, dest_node, weight='length')
route_list.append(route)
fig, ax = ox.plot_graph_route(G, route_list, node_size=0)

我希望在同一张 map 上以不同颜色为每条路线绘制 route_list 中的每条路线。由于 OSMNx 中没有内置函数,有没有办法做到这一点?

我发现解决方案是“绘制图表,然后使用 matplotlib 在顶部手动添加路线”。但无法实现。

最佳答案

编辑:请注意,从 OSMnx 0.15.0 开始,您现在可以轻松地以自己的颜色轻松绘制多条路线:

routes = [route1, route2, route3]
rc = ['r', 'y', 'c']
fig, ax = ox.plot_graph_routes(G, routes, route_colors=rc, route_linewidth=6, node_size=0)

osmnx plot multiple routes each with their own color

有关详细信息,请参阅 the docs或者这个 usage example笔记本。

===============================

2018 年的原始答案:

值得注意的是,从 v0.8.2 开始,此功能现在存在于 OSMnx 中。示例 here绘制多条路线,每条路线都有自己的颜色。

import networkx as nx
import osmnx as ox
ox.config(log_console=True, use_cache=True)

G = ox.graph_from_place('Piedmont, CA, USA', network_type='drive')

# pick 4 random nodes as origins/destinations for the 2 routes
orig1 = list(G.nodes())[0]
dest1 = list(G.nodes())[-1]
orig2 = list(G.nodes())[50]
dest2 = list(G.nodes())[-50]

# calculate shortest paths for the 2 routes
route1 = nx.shortest_path(G, orig1, dest1, weight='length')
route2 = nx.shortest_path(G, orig2, dest2, weight='length')

# create route colors
rc1 = ['r'] * (len(route1) - 1)
rc2 = ['b'] * len(route2)
rc = rc1 + rc2
nc = ['r', 'r', 'b', 'b']

# plot the routes
fig, ax = ox.plot_graph_routes(G, [route1, route2], route_color=rc, orig_dest_node_color=nc, node_size=0)

OSMnx plot multiple street network routes each with own color

关于python - 使用 OSMNx 绘制多条路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51258029/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com