使用networkx和gexf格式时的节点位置 [英] Node location when using networkx and gexf format

查看:123
本文介绍了使用networkx和gexf格式时的节点位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用networkx创建一个diGraph对象,用节点和边(包括几个特征)填充它,然后编写一个我要用Gephi可视化的gefx文件.

I'm using networkx to create a diGraph object, populate it with nodes and edges (including several characteristics), and then write a gefx file that I'm visualising with Gephi.

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文件有一个viz标签(

This process works perfectly. Now I need to assign locations to the nodes. I have seen that networkx can somehow do it (http://networkx.github.com/documentation/latest/examples/drawing/house_with_colors.html) and also I know there is a viz tag for gexf files (http://gexf.net/format/viz.html). I have a dictionary with the nodes names and their coordinates. Any idea to put all this together?

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

My option until now is to read the gexf file already generated, look for the nodes and create the viz:position tag.

但是,它不是很有效,我想在添加节点时直接采用某种方式.

However, it isn't very effective and I would like to somehow do it directly when adding the nodes.

推荐答案

每个节点的节点数据都可以作为Python字典使用. 这是显示GEXF节点viz数据如何存储和操作的示例.

The node data is available as a Python dictionary for each node. Here is an example showing how the GEXF node viz data is stored and manipulated.

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>

这篇关于使用networkx和gexf格式时的节点位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆