gpt4 book ai didi

python - 如何使 Networkx 生成具有排序节点的 GML 文件

转载 作者:太空宇宙 更新时间:2023-11-04 01:08:35 25 4
gpt4 key购买 nike

我有以下代码:

#!/usr/bin/env python
import networkx as nx
def main():
"""docstring for main"""
outerdict = {"A":["a1","a2","a3"], "B":["b1","b2","b3","b5","b6", "b7"], "C":["c2","c3"], "D":["d1","d2","d3"]}
keynode = "Z"

colorlist = [ "#beaed4", "#fdc086", "#ffff99", "#386cb0","#f0027f"]
G = nx.Graph()
G.add_node(keynode,graphics={"fill":"#7fc97f","w":27,"h":27, "d":27})
for i,ky in enumerate(outerdict):
nodes = outerdict[ky]
nodecol = colorlist[i]
print ky, nodecol
for node in nodes:
G.add_node(node,graphics={"fill":nodecol,"w":27,"h":27,"d":27})
G.add_edge(keynode, node, value = 10)

outfile = "test.gml"
nx.write_gml(G,outfile)


if __name__ == '__main__':
main()

它生成以下 GML 文件:

graph [
node [
id 0
label "b6"
graphics [
h 27
d 27
w 27
fill "#ffff99"
]
]
node [
id 1
label "a1"
graphics [
h 27
d 27
w 27
fill "#beaed4"
]
]
node [
id 2
label "b5"
graphics [
h 27
d 27
w 27
fill "#ffff99"
]
]
node [
id 3
label "a3"
graphics [
h 27
d 27
w 27
fill "#beaed4"
]
]
node [
id 4
label "a2"
graphics [
h 27
d 27
w 27
fill "#beaed4"
]
]
node [
id 5
label "b7"
graphics [
h 27
d 27
w 27
fill "#ffff99"
]
]
node [
id 6
label "b1"
graphics [
h 27
d 27
w 27
fill "#ffff99"
]
]
node [
id 7
label "b2"
graphics [
h 27
d 27
w 27
fill "#ffff99"
]
]
node [
id 8
label "b3"
graphics [
h 27
d 27
w 27
fill "#ffff99"
]
]
node [
id 9
label "c3"
graphics [
h 27
d 27
w 27
fill "#fdc086"
]
]
node [
id 10
label "c2"
graphics [
h 27
d 27
w 27
fill "#fdc086"
]
]
node [
id 11
label "Z"
graphics [
h 27
d 27
w 27
fill "#7fc97f"
]
]
node [
id 12
label "d2"
graphics [
h 27
d 27
w 27
fill "#386cb0"
]
]
node [
id 13
label "d3"
graphics [
h 27
d 27
w 27
fill "#386cb0"
]
]
node [
id 14
label "d1"
graphics [
h 27
d 27
w 27
fill "#386cb0"
]
]
edge [
source 0
target 11
value 10
]
edge [
source 1
target 11
value 10
]
edge [
source 2
target 11
value 10
]
edge [
source 3
target 11
value 10
]
edge [
source 4
target 11
value 10
]
edge [
source 5
target 11
value 10
]
edge [
source 6
target 11
value 10
]
edge [
source 7
target 11
value 10
]
edge [
source 8
target 11
value 10
]
edge [
source 9
target 11
value 10
]
edge [
source 10
target 11
value 10
]
edge [
source 11
target 12
value 10
]
edge [
source 11
target 13
value 10
]
edge [
source 11
target 14
value 10
]
]

注意节点没有按顺序排列。例如 a1 位于 b1b5 之间。这使得情节看起来像这样:

enter image description here

我期望图形是根据循环和初始数据结构 outderdict 按顺序排序。这样黄色节点将与其他黄色节点依次排列在一起。

如何使用 Networkx 对 GML 文件进行排序?其他解决方案(例如 Igraph),只要在 Python 框架内,都欢迎。

最佳答案

自 2015 年 1 月 1 日起,NetworkX 中提供了有序图数据结构。OrderedGraph 类将按照添加顺序从 NetworkX 数据结构中输出节点和边。
您需要在 https://github.com/networkx/networkx/ 获取最新的开发版本为以下工作。

import networkx as nx
outerdict = {"A":["a1","a2","a3"], "B":["b1","b2","b3","b5","b6", "b7"], "C":["c2","c3"], "D":["d1","d2","d3"]}
keynode = "Z"
colorlist = [ "#beaed4", "#fdc086", "#ffff99", "#386cb0","#f0027f"]
G = nx.OrderedGraph()
G.add_node(keynode,graphics={"fill":"#7fc97f","w":27,"h":27, "d":27})
for i,ky in enumerate(outerdict):
nodes = outerdict[ky]
nodecol = colorlist[i]
print ky, nodecol
for node in nodes:
G.add_node(node,graphics={"fill":nodecol,"w":27,"h":27,"d":27})
G.add_edge(keynode, node, value = 10)
outfile = "test.gml"
nx.write_gml(G,outfile)

请注意,当您遍历“enumerate(outerdict)”时,您的“outerdict”数据结构的出现顺序可能与您初始化它的顺序不同。如果节点“A”、“B”、“C”和“D”的顺序对您很重要,那么您将需要更改将它们添加到图表中的方式,以确保它们按该顺序插入。

关于python - 如何使 Networkx 生成具有排序节点的 GML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29027335/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com