【问题标题】:Networkx not coloring edges correctly by attributeNetworkx 没有按属性正确着色边缘
【发布时间】:2021-04-01 23:46:50
【问题描述】:

我试图在networkx中描绘一个传输网络,到目前为止我一直做得很好,但问题如下: 我从一个txt文件中加载了一个edgelist,格式如下:

61  1
54  79
66  134
68  57

等等。此外,为了在正确的位置绘制网络节点,我已经加载并添加到 Graph 一个带有节点坐标的 txt,格式如下:

1   478947.434  4204495.502
2   478909.145  4204244.629
3   479065.936  4204709.003
4   478880.827  4204297.676
5   478993.409  4204167.957

之后,我运行以下脚本,我的网络按位置正确描绘。

import networkx as nx
import numpy as py
import copy
import matplotlib.pyplot as plt
import xlwings as xw
from xlwings import Book, Range

pos={}
with open(r'C:\Users\alexl\Documents\new_python\nodes_coordinates.txt') as f:
    for line in f:
        node, x, y = line.split()
        pos[node] = float(x), float(y)

print(pos)
network0 = nx.read_edgelist(r'C:\Users\alexl\Documents\new_python\init_edgelist.txt', create_using = nx.OrderedDiGraph)

nx.draw(network0, pos = pos, with_labels = True, edge_color = G, edge_cmap = plt.cm.Reds, connectionstyle = 'arc3,rad=0.1')
         

plt.show()

现在,我想要的是正确地为每条边分配“权重”,并通过此属性对其进行着色。对于每条边,都有 3 种可能的结果,0、1 和 2,所以我想要 3 种不同的颜色,例如,红色代表 0,绿色代表 1,蓝色代表 2。我拥有的是这样的 Python 字典:

{0: 1.0, 1: 1.0, 2: 0.0, 3: 1.0, 4: 1.0, 5: 0.0, 6: 0.0, 7: 0.0, 8: 1.0,...

等。第一个值 (1.0) 对应于上面 edgelist 的第一个边缘,第二个 (1.0) 对应于第二个,依此类推。但我看到 networkx 不理解这一点并且没有正确着色我的网络。我将字典转换为列表并执行了以下操作:

var1 = G.values()
list1 = list(var1)
col = []
for n in range(len(list1)):
    if list1[n] == 0:
        col.append('b')
    elif list1[n] == 1:
        #col.append('r')
    else:
        #col.append('g') 
nx.draw(network0, pos = pos, with_labels = True, edge_color = col, edge_cmap = plt.cm.Reds, connectionstyle = 'arc3,rad=0.1')

我检查了结果,但对应的不是正确的。我认为,如果列表中有 32 个“0”,它会使用我分配给它的颜色为 32 个边缘着色,但不是正确的边缘。 (但每次我运行它时,相同的 32 条边都以这种方式着色,所以它可能不是随机的。)我想知道什么是不正确的。我也尝试将它作为字典(不转换为列表),但实际上颜色超过 3,我不知道为什么。 我是否应该尝试将这些值(0、1、1、2 等)添加到 edgelist.txt 以正确完成分配?

【问题讨论】:

  • 变量G是什么?
  • G 是这样的字典:{0: 1.0, 1: 1.0, 2: 0.0, 3: 1.0, 4: 1.0, 5: 0.0, 6: 0.0, 7: 0.0, 8: 1.0,...。我想要的是一对一地分配边缘中的字典值。边缘列表中的字典“项目”与边缘一样多,我希望在着色时将第一个值(1.0)分配给边缘列表的第一条边缘。此外,根据字典值(0.0、1.0 或 2.0)进行着色。

标签: python dataframe graph networkx graph-coloring


【解决方案1】:

传递给绘图函数的边的顺序很重要。
您需要确保您的edge_color 列表(col) 与network0.edges 的顺序相同,但事实并非如此。

发生这种情况是因为 network0.edges 与您的 init_edgelist.txt 文件的顺序不同,正如您可能假设的那样。

要检查这一点,请尝试打印 network0.edges 并进行比较。

解决方案

相反,我建议在创建network0 时向边缘添加具有假装颜色的边缘权重,如下所示:

G = {0: 1.0, 1: 1.0, 2: 0.0, 3: 1.0, 4: 1.0, 5: 0.0, 6: 0.0, 7: 0.0, 8: 1.0}
list1 = list(G.values())
col = []
for n in range(len(list1)):
    if list1[n] == 0:
        col.append('b')
    elif list1[n] == 1:
        col.append('r')
    else:
        col.append('g') 


network0 = nx.OrderedDiGraph()
i = 0
with open(r'init_edgelist.txt') as f:
    for line in f:
        n1, n2 = line.split()
        network0.add_edge(n1, n2, color = col[i])
        i += 1

然后你只需要提取颜色并绘制:

colors = [network0[u][v]['color'] for u,v in network0.edges()]

nx.draw(network0, pos = pos, with_labels = True, edge_color = colors, edge_cmap = plt.cm.Reds, connectionstyle='arc3,rad=0.1')

完整代码

import networkx as nx
import numpy as py
import copy
import matplotlib.pyplot as plt
import xlwings as xw
from xlwings import Book, Range

pos={}
with open(r'nodes_coordinates.txt') as f:
    for line in f:
        node, x, y = line.split()
        pos[node] = float(x), float(y)

#print(pos)

G = {0: 1.0, 1: 1.0, 2: 0.0, 3: 1.0, 4: 1.0, 5: 0.0, 6: 0.0, 7: 0.0, 8: 1.0}
list1 = list(G.values())
col=[]
for n in range(len(list1)):
    if list1[n] == 0:
        col.append('b')
    elif list1[n] == 1:
        col.append('r')
    else:
        col.append('g') 

network0 = nx.OrderedDiGraph()

i = 0
with open(r'init_edgelist.txt') as f:
    for line in f:
        n1, n2 = line.split()
        network0.add_edge(n1,n2, color=col[i])
        i += 1

#print(network0.edges())

colors = [network0[u][v]['color'] for u,v in network0.edges()]

nx.draw(network0,pos=pos,with_labels=True,edge_color=colors,edge_cmap=plt.cm.Reds, connectionstyle='arc3,rad=0.1')

【讨论】:

  • 检查这个最后一个编辑,另一个在代码中有一个小错误
  • 首先,非常感谢您尝试解决我的问题。我写了最后一个脚本,但没有正确返回边缘,不幸的是...... initial_edgelist.txt 的前 5 行是:61 1 54 79 66 134 68 57 65 86。当我打印 network0.edges 时,我看到了这个:('61', '1'), ('61', '69'), ('61', '60'), ('1', '128'), ('1', '75')。我知道这种表示可能会更好,因为它首先所有边都以 61 开头,然后所有边都以 1 开头,等等。但我不想要这个。非常感谢您回答了我的问题!我对networkx很陌生。
  • 但是你有没有检查过边缘是否有你假装的颜色?
  • 我的意思是这个答案是为了回答你的问题,我只是给你代码背后的问题
  • 嗯,我的错,我没有尝试!你的代码做了我想做的事!非常感谢。
猜你喜欢
  • 2014-05-22
  • 2016-06-01
  • 2013-07-12
  • 1970-01-01
  • 1970-01-01
  • 2019-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多