【问题标题】:Coloring nodes according to an attribute in osmnx根据 osmnx 中的属性为节点着色
【发布时间】:2020-07-09 23:11:46
【问题描述】:

我试图根据特定属性为我的网络节点着色,但是函数 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')

【问题讨论】:

  • 您已将属性'r' 添加到您的数据框nodes 而不是您的图表G_nx

标签: python networkx osmnx


【解决方案1】:

如果换行

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 的文档)——即使我不明白你为什么以如此复杂的方式分配随机值,但我想这只是为了有一个最小的、可重现的例子。

【讨论】:

  • 是的,这只是示例,r_total 实际上是一个包含特定数据的列表。谢谢你的回答
猜你喜欢
  • 1970-01-01
  • 2022-11-02
  • 1970-01-01
  • 2015-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多