2013-01-21 23 views
4

我正在使用networkx创建一个diGraph对象,使用节点和边缘(包括几个特征)填充它,然后编写一个gefx文件,我正在使用Gephi进行可视化。使用networkx和gexf格式时的节点位置

import networkx as nx 
dg = nx.DiGraph() 
dg.add_node(attribute1, 2, etc...) 
dg.add_edge(attribute1, 2, etc...) 
nx.write_gexf("output.gexf") 

该过程完美地工作。现在我需要为节点分配位置。我看到,networkx可以以某种方式做到这一点(http://networkx.github.com/documentation/latest/examples/drawing/house_with_colors.html),我也知道有一个gexf文件的标签(http://gexf.net/format/viz.html)。我有一个包含节点名称和坐标的字典。任何想法把所有这些放在一起?

我到目前为止的选择是读取已生成的gexf文件,查找节点并创建viz:position标记。

但是,它不是非常有效,我想以某种方式直接添加节点时做。

回答

6

节点数据可用作每个节点的Python字典。 下面是一个例子,显示了GEXF节点数据如何存储和操作。

In [1]: import sys 

In [2]: import urllib2 

In [3]: import networkx as nx 

In [4]: data = urllib2.urlopen('http://gexf.net/data/viz.gexf') 

In [5]: G = nx.read_gexf(data) 

In [6]: print G.node['a'] 
{'viz': {'color': {'a': 0.6, 'r': 239, 'b': 66, 'g': 173}, 'position': {'y': 40.109245, 'x': 15.783598, 'z': 0.0}, 'size': 2.0375757}, 'label': 'glossy'} 

In [7]: G.node['a']['viz']['position']['x']=10 

In [8]: G.node['a']['viz']['position']['y']=20 

In [9]: print G.node['a'] 
{'viz': {'color': {'a': 0.6, 'r': 239, 'b': 66, 'g': 173}, 'position': {'y': 20, 'x': 10, 'z': 0.0}, 'size': 2.0375757}, 'label': 'glossy'} 

In [10]: nx.write_gexf(G,sys.stdout) 
<?xml version="1.0" encoding="utf-8"?><gexf xmlns:ns0="http://www.gexf.net/1.1draft/viz" version="1.1" xmlns="http://www.gexf.net/1.1draft" xmlns:viz="http://www.gexf.net/1.1draft/viz" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3.org/2001/XMLSchema-instance"> 
    <graph defaultedgetype="undirected" mode="static"> 
    <nodes> 
     <node id="a" label="glossy"> 
     <ns0:color b="66" g="173" r="239" /> 
     <ns0:size value="2.0375757" /> 
     <ns0:position x="10" y="20" z="0.0" /> 
     </node> 
    </nodes> 
    <edges /> 
    </graph> 
</gexf>