gpt4 book ai didi

python - Osmnx 绘制多条图路线

转载 作者:太空宇宙 更新时间:2023-11-03 21:31:25 25 4
gpt4 key购买 nike

如果我在不同的 map 上绘制每条路线,结果是完美的。但是,我想要的是在单个 map 上绘制多条路线。

我的代码是:

import osmnx as ox, networkx as nx, matplotlib.pyplot as plt, math as m
from haversine import haversine

org = (27.5075, 77.6779)
dst = (27.5067, 77.6694)
dist_fl = (int(haversine(org, dst)*1000))*2
G = ox.graph_from_point(org, simplify=False, distance=dist_fl)
orig_node = ox.get_nearest_node(G, org, method='euclidean')
target_node = ox.get_nearest_node(G, dst, method='euclidean')

nodes, edges = ox.graph_to_gdfs(G)
streets_per_node = ox.count_streets_per_node(G)
route = nx.shortest_path(G, source=orig_node, target=target_node,
weight='length', method='dijkstra')
node_route = nodes.loc[route]

route_list = [[1352186296, 1352186345], [1352186345, 1352186150, 1350260490,
1352186112, 1352186349, 1350260516, 4753659536L, 4753498382L], [4753498382,
4753498383L, 4753498390L], [4753498390, 4753498391L, 4753498392L,
4753498393L, 4753498394L], [4753498394, 4753498395L, 4753498396L,
4753498397L, 4753498398L], [4753498398, 4753478108L], [4753478108,
4753498399L, 4753498400L, 4753498401L], [4753498401, 4753498402L,
4753498403L], [4753498403, 4753498404L, 4753498405L, 4753657826L,
4753498406L, 4753498407L, 4753498408L, 4753498409L], [4753498409, 4753498410L, 4753498411L, 4753498412L], [4753498412, 3803339674L], [3803339674, 1352186110, 1352186253, 2265692728L], [2265692728, 1352186308, 1352186133, 1352186328, 1352186316, 1352186071, 1352186187, 1352186095], [1352186095, 1352186361, 1352186263]]

coord_list =[(77.6776527, 27.5069438), (77.6775911, 27.5061476), (77.6747421, 27.5061048), (77.6747447, 27.5065153), (77.6742216, 27.5065135), (77.6741438, 27.507339), (77.6740672, 27.5073159), (77.6738192, 27.5079793), (77.6736687, 27.5079667), (77.6726402, 27.5084229), (77.6720677, 27.5081814), (77.6717673, 27.5093129), (77.6704672, 27.5094812), (77.6690214, 27.507629)]

color_list = ['green', 'green', 'green', 'green', 'red', 'green', 'green', 'green', 'green', 'green', 'green', 'green', 'red', 'red']

fig, ax = ox.plot_graph_routes(G, route_list, fig_height=10, fig_width=10, save=True, filename='outfile_data', show=False, close=False, axis_off=False, edge_linewidth=1, node_size=10, route_color = color_list)

plt.show()

Screenshot

最佳答案

一条路线由多条线路组成。您必须为 route 的每条线创建颜色列表。然后根据其线路数更改下一条路线的颜色。例如。如果你有两条路由,比如 sp1 和 sp2,它们包含连续的 OSMids,那么,你必须编写如下内容:

c1 = (len(sp1)-1) * ['r']
c2 = (len(sp2)-1) * ['g']
color_list = c1 + c2
ox.plot.plot_graph_routes(G,[sp1,sp2],route_color = color_list)

---------------------用以下代码替换颜色列表----

list_of_colors = [ 'green', 'purple', 'black', 'blue', 'yellow', 'red', 'orange'] ## add more colour if you have more routes

color_list = []

for i in range(len(route_list)):
num_lines = len(route_list[i]) - 1
color_elements = num_lines * [list_of_colors[i]]
color_list = color_list + color_elements

print(color_list)
ox.plot.plot_graph_routes(G,route_list,route_color = color_list)

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

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