Fork me on GitHub

Python生成gexf文件并导入gephi做网络图分析

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:

<?xml version='1.0' encoding='utf-8'?>
<gexf xmlns:viz="http://www.gexf.net/1.2draft/viz" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.gephi.org/gexf/1.2draft" xmlns:ns0="xsi" version="1.2" ns0:schemaLocation="http://www.gephi.org/gexf/1.1draft http://gephi.org/gexf/1.2draft.xsd">
  <meta lastmodified="2015-08-24">
    <creator>Gephi.org</creator>
    <description>A Web network</description>
  </meta>
  <graph defaultedgetype="directed" label="A Web network" mode="static" timeformat="double">
    <attributes class="node" mode="static">
      <attribute id="0" title="url" type="string"/>
      <attribute id="1" title="indegree" type="float"/>
      <attribute id="2" title="frog" type="boolean">
        <default>true</default>
      </attribute>
    </attributes>
    <nodes>
      <node id="0" label="Gephi">
        <attvalues>
          <attvalue for="0" value="http://gephi.org"/>
          <attvalue for="1" value="1"/>
        </attvalues>
      </node>
      <node id="1" label="Webatlas">
        <attvalues>
          <attvalue for="0" value="http://webatlas.fr"/>
          <attvalue for="1" value="2"/>
        </attvalues>
      </node>
      <node id="2" label="RTGI">
        <attvalues>
          <attvalue for="0" value="http://rtgi.fr"/>
          <attvalue for="1" value="1"/>
        </attvalues>
      </node>
      <node id="3" label="BarabasiLab">
        <attvalues>
          <attvalue for="0" value="http://barabasilab.com"/>
          <attvalue for="1" value="1"/>
          <attvalue for="2" value="false"/>
        </attvalues>
      </node>
    </nodes>
    <edges>
      <edge id="0" source="0" target="1" weight="1"/>
      <edge id="1" source="0" target="2" weight="1"/>
      <edge id="2" source="1" target="0" weight="1"/>
      <edge id="3" source="2" target="1" weight="1"/>
      <edge id="4" source="0" target="3" weight="1"/>
    </edges>
  </graph>
</gexf>


导入到Gephi中:

posted @ 2015-08-24 16:30  ZHK的博客  阅读(17656)  评论(0编辑  收藏  举报