根据边缘属性为OSMnx图中的边缘着色

时间:2020-07-26 15:54:25

标签: python networkx osmnx

我想创建一个国家/地区的道路地图,并根据其“高速公路”属性为边缘着色,以使高速公路为黄色,主干绿色等。

但是,当遵循osmnx示例文件并尝试复制时,我收到以下错误消息: 输入:

ec = ox.plot.get_edge_colors_by_attr(graph, attr='highway', cmap='plasma_r')

输出:

TypeError: '<=' not supported between instances of 'str' and 'list'

我假设这是因为“高速公路”不是数字变量吗? 这是我目前用于图形的代码

    graph = ox.io.load_graphml("graph.graphml")
    nodes, streets = ox.graph_to_gdfs(graph)
    streets.head()

输出:


       osmid  oneway lanes  ref    highway    junction    length                                           geometry              name maxspeed bridge tunnel access width service           u           v  key
0  659557392    True     1  410  secondary  roundabout    48.672  LINESTRING (-21.93067 64.05665, -21.93067 64.0...               NaN      NaN    NaN    NaN    NaN   NaN     NaN  6175252481  6175252453    0
1  659557393   False     2  410  secondary         NaN   132.007  LINESTRING (-21.93067 64.05665, -21.93057 64.0...   Kaldárselsvegur      NaN    NaN    NaN    NaN   NaN     NaN  6175252481  6275284224    0
2   48547677    True   NaN  430  secondary         NaN   237.337  LINESTRING (-21.72904 64.13621, -21.72959 64.1...     Skyggnisbraut       50    NaN    NaN    NaN   NaN     NaN  5070446594   616709938    0
3  160506796   False   NaN  430  secondary         NaN  2892.051  LINESTRING (-21.72904 64.13621, -21.72848 64.1...  Úlfarsfellsvegur       70    NaN    NaN    NaN   NaN     NaN  5070446594    56620274    0
4  157591872    True     2   41      trunk  roundabout    47.075  LINESTRING (-21.93736 64.06693, -21.93730 64.0...        Hlíðartorg       60    NaN    NaN    NaN   NaN     NaN    12886026    12885866    0

1 个答案:

答案 0 :(得分:1)

我认为这是因为“高速公路”不是数字变量吗?

是的。如您在OSMnx docs中所见,ox.plot.get_edge_colors_by_attr函数期望attr参数是“数字边缘属性的名称”。在您的示例中,它不是数字。相反,您可以使用ox.plot.get_colors function为图形中的每种公路类型获取一种颜色,然后根据每种公路类型获取边缘的颜色列表:

import osmnx as ox
import pandas as pd
ox.config(use_cache=True, log_console=True)

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

# get one color for each highway type in the graph
edges = ox.graph_to_gdfs(G, nodes=False)
edge_types = edges['highway'].value_counts()
color_list = ox.plot.get_colors(n=len(edge_types), cmap='plasma_r')
color_mapper = pd.Series(color_list, index=edge_types.index).to_dict()

# get the color for each edge based on its highway type
ec = [color_mapper[d['highway']] for u, v, k, d in G.edges(keys=True, data=True)]
fig, ax = ox.plot_graph(G, edge_color=ec)