1

我想给出将由 plot_graph_routes 创建的每条路线以提供单独的颜色。我可以通过@gboing在这里解释两条路线。但是我有很多路线,所以我确实尝试循环解决它。

G = ox.graph_from_place('München, Oberbayern, Bayern, Deutschland', network_type='drive_service', simplify=True)
G_projected = ox.project_graph(G)
#%%
ox.config(log_console=True, use_cache=True)
colors =  ['b', 'g', 'r', 'c', 'm', 'y', 'k', 'w']
rc = []
nc = []
for r in range(len(routes)-1):
    if r == 0:
        rc.extend(colors[r] * (len(routes[r]) - 1))
    else:
        rc.extend(colors[r] * len(routes[r]))
    nc.extend([colors[r], colors[r]])
fig, ax = ox.plot_graph_routes(G_projected, [routes], fig_height=40,route_color=rc, orig_dest_node_color=nc, node_size=0)

如果我这样做,我会收到以下错误:

---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-32-956a08875e0e> in <module>
      9         rc.extend(colors[r] * len(routes[0][r]))
     10     nc.extend([colors[r], colors[r]])
---> 11 fig, ax = ox.plot_graph_routes(G_projected, [routes[0]], fig_height=40,route_color=rc, orig_dest_node_color=nc, node_size=0)
     12 
     13 

~\Anaconda3\lib\site-packages\osmnx\plot.py in plot_graph_routes(G, routes, bbox, fig_height, fig_width, margin, bgcolor, axis_off, show, save, close, file_format, filename, dpi, annotate, node_color, node_size, node_alpha, node_edgecolor, node_zorder, edge_color, edge_linewidth, edge_alpha, use_geom, orig_dest_points, route_color, route_linewidth, route_alpha, orig_dest_node_alpha, orig_dest_node_size, orig_dest_node_color, orig_dest_point_color)
    732             origin_node = route[0]
    733             destination_node = route[-1]
--> 734             orig_dest_points_lats.append(G.nodes[origin_node]['y'])
    735             orig_dest_points_lats.append(G.nodes[destination_node]['y'])
    736             orig_dest_points_lons.append(G.nodes[origin_node]['x'])

~\Anaconda3\lib\site-packages\networkx\classes\reportviews.py in __getitem__(self, n)
    176 
    177     def __getitem__(self, n):
--> 178         return self._nodes[n]
    179 
    180     # Set methods

TypeError: unhashable type: 'list'

感谢您的任何建议。

4

1 回答 1

0

这是使用您引用的原始示例的多条路线的完整工作示例:

import networkx as nx
import osmnx as ox
import numpy as np

import matplotlib.pylab as plt

ox.config(log_console=True, use_cache=True)

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

available_colors = ['b', 'g', 'r', 'c', 'm', 'y', 'k']

# create Nroutes random routes
Nroutes = 5
routes = []

route_colors = []
node_colors = []
for iroute in range(Nroutes):
    orig, dest = np.random.choice(G.nodes(), 2)
    route = nx.shortest_path(G, orig, dest, weight='length')
    routes.append(route)

    route_color = available_colors[iroute%len(available_colors)]

    node_colors.extend([route_color]*2) # the colors for Origin and Destination of this route

    route_colors.extend([route_color]*(len(route) - 1))

# plot the routes
fig, ax = ox.plot_graph_routes(G, routes, route_color=route_colors, orig_dest_node_color=node_colors, node_size=0)
plt.show()

在此处输入图像描述

于 2019-09-01T19:35:24.340 回答