1

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

我的代码是:

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()

截屏

4

1 回答 1

0

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

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)
于 2019-07-18T08:36:39.370 回答