Gephi是一款优秀的复杂网络分析软件,支持导入多种格式的文件。gexf格式是Gephi 推荐的格式,基于 XML。本文是一个用python写的简单Demo,示例如何生成一个典型的gexf格式文件。代码基于pygexf包(下载地址:https://github.com/paulgirard/pygexf)。 代码很简单不做解释。
Python 代码:
1 import sys,pprint 2 from gexf import Gexf 3 4 5 # test helloworld.gexf 6 gexf = Gexf("Gephi.org","A Web network") 7 graph=gexf.addGraph("directed","static","A Web network") 8 9 atr1 = graph.addNodeAttribute('url',type='string')10 atr2 = graph.addNodeAttribute('indegree',type='float')11 atr3 = graph.addNodeAttribute('frog',type='boolean',defaultValue='true')12 13 tmp = graph.addNode("0","Gephi")14 tmp.addAttribute(atr1,"http://gephi.org")15 tmp.addAttribute(atr2,'1')16 17 tmp = graph.addNode("1","Webatlas")18 tmp.addAttribute(atr1,"http://webatlas.fr")19 tmp.addAttribute(atr2,'2')20 21 tmp = graph.addNode("2","RTGI")22 tmp.addAttribute(atr1,"http://rtgi.fr")23 tmp.addAttribute(atr2,'1')24 25 tmp = graph.addNode("3","BarabasiLab")26 tmp.addAttribute(atr1,"http://barabasilab.com")27 tmp.addAttribute(atr2,'1')28 tmp.addAttribute(atr3,'false')29 30 graph.addEdge("0","0","1",weight='1')31 graph.addEdge("1","0","2",weight='1')32 graph.addEdge("2","1","0",weight='1')33 graph.addEdge("3","2","1",weight='1')34 graph.addEdge("4","0","3",weight='1')35 36 37 output_file=open(".\data.gexf","w")38 gexf.write(output_file)
生成的最终文件data.gexf:
Gephi.org A Web network true