根据 osmnx 中的属性为节点着色
Coloring nodes according to an attribute in osmnx
我试图根据特定属性为我的网络节点着色,但是函数 ox.plot.get_node_colors_by_attr()
不起作用。
错误是:ValueError: There are no attribute values.
代码如下:
import networkx as nx
import numpy as np
import osmnx as ox
ox.config(use_cache=True, log_console=True)
city = 'Portugal, Lisbon'
G = ox.graph_from_place(city, network_type='drive', simplify=True)
G_nx = nx.relabel.convert_node_labels_to_integers(G)
nodes, edges = ox.graph_to_gdfs(G_nx, nodes=True, edges=True)
r_total = np.random.random(len(G_nx))
nodes['r'] = nodes.index.map(lambda x: r_total[x])
nodes.head()
nc = ox.plot.get_node_colors_by_attr(G_nx, attr='r')
如果替换行
nodes['r'] = nodes.index.map(lambda x: r_total[x])
nodes.head()
与
nx.set_node_attributes(G_nx, {x:r_total[x] for x in nodes.index}, name='r')
你的代码应该可以工作(set_node_attributes
的文档)——即使我不明白你为什么以如此复杂的方式分配随机值,但我想这只是为了有一个最小的、可重现的例子.
我试图根据特定属性为我的网络节点着色,但是函数 ox.plot.get_node_colors_by_attr()
不起作用。
错误是:ValueError: There are no attribute values.
代码如下:
import networkx as nx
import numpy as np
import osmnx as ox
ox.config(use_cache=True, log_console=True)
city = 'Portugal, Lisbon'
G = ox.graph_from_place(city, network_type='drive', simplify=True)
G_nx = nx.relabel.convert_node_labels_to_integers(G)
nodes, edges = ox.graph_to_gdfs(G_nx, nodes=True, edges=True)
r_total = np.random.random(len(G_nx))
nodes['r'] = nodes.index.map(lambda x: r_total[x])
nodes.head()
nc = ox.plot.get_node_colors_by_attr(G_nx, attr='r')
如果替换行
nodes['r'] = nodes.index.map(lambda x: r_total[x])
nodes.head()
与
nx.set_node_attributes(G_nx, {x:r_total[x] for x in nodes.index}, name='r')
你的代码应该可以工作(set_node_attributes
的文档)——即使我不明白你为什么以如此复杂的方式分配随机值,但我想这只是为了有一个最小的、可重现的例子.