【问题标题】:wrong result while defining node size based on node degree in networkx根据networkx中的节点度定义节点大小时出现错误的结果
【发布时间】:2017-04-19 07:53:27
【问题描述】:

我知道之前有人问过类似的问题,但我的问题有点复杂。我正在尝试根据它们的出度来定义networkx图的节点大小,但是会有些混乱。这是我的代码:

nodes = pd.read_csv('nodes_1984.csv')
edges = pd.read_csv('edges_1984.csv')
nodes.head()

this how my data containing nodes looks like (click)

network=nx.DiGraph() 
for row in nodes.iterrows():
    network.add_node(row[1][0], Label=row[1][1], region=row[1][2], pos=(row[1][4], row[1][3]))
for row in edges.iterrows():
    network.add_edge(row[1][0],row[1][1], weight=row[1][3])

pos=nx.get_node_attributes(network,'pos')

d_out = network.out_degree()  #dictionary of {nodes:out_degree_values}
d_out_val = []  #creating a list which contains values of out_degree of nodes and sorting them in the same order as they were in the d_out dict

for w in sorted(d_out.keys()):
    d_out_val.append(d_out[w])

d_out_val

plt.figure(figsize=(32,28)) #plotting my network
nx.draw(network, pos, with_labels=True, node_size=[s * 100 for s in d_out_val], node_color="w")
plt.show()

结果是我在图中节点的大小与这些节点的出度值不对应,尽管我在上面对包含节点度值的列表 (d_out_val) 进行了排序。 你能告诉我这个问题可以解决什么吗?

【问题讨论】:

    标签: python python-2.7 networkx


    【解决方案1】:

    nx.draw 传递给nodelist 参数,您可以在该参数中提供节点的排序列表。所以不是

    nx.draw(network, pos, with_labels=True, node_size=[s * 100 for s in d_out_val], node_color="w")
    

    你将拥有:

    nx.draw(network, pos, nodelist=sorted(network.nodes()), with_labels=True, node_size=[s * 100 for s in d_out_val], node_color="w")
    

    如果您不提供 nodelist 参数,networkx 将使用 network.nodes()(它不像您的 node_size 列表那样排序)。

    【讨论】:

      猜你喜欢
      • 2013-05-10
      • 1970-01-01
      • 2019-08-23
      • 1970-01-01
      • 2018-12-10
      • 2012-11-11
      • 2022-11-18
      • 1970-01-01
      相关资源
      最近更新 更多