【问题标题】:How do I specify colors for the individual edges of a route in OSMNX?如何在 OSMNX 中为路线的各个边缘指定颜色?
【发布时间】:2020-10-16 20:40:47
【问题描述】:

我正在尝试通过沿该路线的各个边缘的属性为路线着色,这在早期版本的 osmnx 中使用以下语法是可能的:

# just an example graph
G = ox.graph_from_point((42.35083, -71.089473), network_type='drive', dist_type='network',
                        simplify=False, dist=100)
# example route
route = [61354688, 7542547660, 5674166794, 61354678]
# color for each edge (4 nodes, 3 edges)
rcs = ['r', 'b', 'r']
ox.plot_graph_route(G, route, route_color=rcs)

这会引发以下错误: ValueError: 'c' argument has 3 elements, which is not acceptable for use with 'x' with size 2, 'y' with size 2.

我不确定以前是如何实现的,但它似乎可以采用与路线中的边缘相同数量的元素并相应地颜色的列表。有没有一种新的方法来实现这一点?似乎可以通过特定属性为整个图表着色,但不能通过单个路线。

任何帮助将不胜感激!

【问题讨论】:

    标签: osmnx


    【解决方案1】:

    正如您在docs 中看到的那样,plot_graph_route 函数将route_color 参数作为字符串 来定义绘制路线的颜色。如果要为每条边赋予自己的颜色,则需要在图形顶部手动绘制路线。这是一个示例,大致改编自 OSMnx source code

    import osmnx as ox
    ox.config(use_cache=True)
    
    # example graph and route
    G = ox.graph_from_point((42.35083, -71.089473), network_type='drive', dist_type='network',
                            simplify=False, dist=100)
    route = [61354688, 7542547660, 5674166794, 61354678]
    rcs = ['r', 'b', 'r']
    node_pairs = zip(route[:-1], route[1:])
    
    # plot graph
    fig, ax = ox.plot_graph(G, show=False, close=False)
    
    # then plot colored route segments on top of it
    for (u, v), rc in zip(node_pairs, rcs):
        data = min(G.get_edge_data(u, v).values(), key=lambda d: d["length"])
        if "geometry" in data:
            x, y = data["geometry"].xy
        else:
            x = G.nodes[u]["x"], G.nodes[v]["x"]
            y = G.nodes[u]["y"], G.nodes[v]["y"]
        ax.plot(x, y, color=rc, lw=5)
    

    【讨论】:

      猜你喜欢
      • 2019-10-30
      • 1970-01-01
      • 2020-12-02
      • 2021-07-26
      • 1970-01-01
      • 1970-01-01
      • 2015-04-06
      • 2018-05-03
      • 2015-03-27
      相关资源
      最近更新 更多