从graphml导入图形,使用NetworkX恢复节点位置 [英] Import graph from graphml restoring node positions with NetworkX

查看:184
本文介绍了从graphml导入图形,使用NetworkX恢复节点位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想加载它并将它的节点绘制在与之前相同的位置。保存在graphml文件中的数据由原始类型组成,而由 mathplotlib 用于绘制图形的数据是一个字典 {string:array([x, y]),dtype = float32} ,其中数组最可能是 numpy array。



我使用NetworkX 1.9.1,这是我的代码。我想这个问题可能是由于迭代造成的。

 将networkx导入为nx 
导入matplotlib .pyplot as plt
import numpy as np

#从graphml文件中加载图形
def load_graph(file_path):
G = nx.read_graphml(file_path)
返回G

def show_graph(G):
#用于创建结构,计算无用
pos = nx.circular_layout(G)

print(pos)

在pos.items()中为(node,node_pos)添加结构(不产生预期结果)

node_pos [0] = G.node [节点] ['x']
node_pos [1] = G.node [节点] ['y']

# $ b print(pos)
plt.clf()
nx.draw_networkx(G,pos)
plt.show()

file_path ='netw_a.graphml '
A = load_graph(file_path)
show_graph(A)

一个用于导入的测试文件

 < graphml xmlns =http://graphml.graphdrawing.org/xmlnsxmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexsi:schemaLocation =http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd\"> 
< graph edgedefault =undirected>
< data key =d0> path_graph(4)< / data>
< node id =0>
< data key =d1> 0.394087123189< / data>
< data key =d2> 0.0< / data>
< / node>
< node id =1>
< data key =d1> 0.743297296307< / data>
< data key =d2> 0.402465740641< / data>
< / node>
< node id =2>
< data key =d1> 0.529781867707< / data>
< data key =d2> 0.942892202945< / data>
< / node>
< node id =3>
< data key =d1> 0.0< / data>
< data key =d2> 1.0< / data>
< / node>
< edge source =0target =1/>
< edge source =1target =2/>
< edge source =2target =3/>
< /图表>
< / graphml>


解决方案

将x和y值分配给字典在[1]中:pos = {}

In [2]:for n ,数据在G.node.items()中:
...:pos [n] =(data ['x'],data ['y'])
...:


I'm saving a graph with node positions in graphml format.

I want to load it and draw its nodes in the same positions as before. Data saved in the graphml file consists of primitive types, whereas data used by mathplotlib to draw the graph is a dictionary {string: array([x, y]), dtype=float32}, where the array is most probably a numpy array.

I'm using NetworkX 1.9.1, and this is my code. I guess the problem may be due messy iteration. Plus, I can't build from scratch the structure used to draw positions.

import networkx as nx
import matplotlib.pyplot as plt
import numpy as np

#load the graph from the graphml file
def load_graph(file_path):
    G = nx.read_graphml(file_path)
    return G

def show_graph(G):
    #used to create the structure, computation useless
    pos = nx.circular_layout(G)

    print(pos)

    #try to refill the structure (don't produce expected results)
    for (node, node_pos) in pos.items():
        node_pos[0] = G.node[node]['x']
        node_pos[1] = G.node[node]['y']

    #draw the graph
    print(pos)
    plt.clf()
    nx.draw_networkx(G, pos)
    plt.show()

file_path = 'netw_a.graphml'
A = load_graph(file_path)
show_graph(A)

Here is a test file for import

<graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
  <key attr.name="x" attr.type="double" for="node" id="d2" />
  <key attr.name="y" attr.type="double" for="node" id="d1" />
  <key attr.name="name" attr.type="string" for="graph" id="d0" />
  <graph edgedefault="undirected">
    <data key="d0">path_graph(4)</data>
    <node id="0">
      <data key="d1">0.394087123189</data>
      <data key="d2">0.0</data>
    </node>
    <node id="1">
      <data key="d1">0.743297296307</data>
      <data key="d2">0.402465740641</data>
    </node>
    <node id="2">
      <data key="d1">0.529781867707</data>
      <data key="d2">0.942892202945</data>
    </node>
    <node id="3">
      <data key="d1">0.0</data>
      <data key="d2">1.0</data>
    </node>
    <edge source="0" target="1" />
    <edge source="1" target="2" />
    <edge source="2" target="3" />
  </graph>
</graphml>

解决方案

Assign the x and y values to the a dictionary with node keys

In [1]: pos = {}

In [2]: for n,data in G.node.items():
   ...:     pos[n] = (data['x'],data['y'])
   ...:  

这篇关于从graphml导入图形,使用NetworkX恢复节点位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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