使用NetworkX将图形导出到带有节点位置的graphml [英] Export graph to graphml with node positions using NetworkX

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

问题描述



我有一个图表,我需要组织位置,然后导出为graphml格式。



我试过这个问题中的代码。它不起作用,这是我的例子

  import networkx as nx 
import matplotlib.pyplot as plt

G = nx.read_graphml(colored.graphml)

pos = nx.spring_layout(G)#快速定位示例
nx.set_node_attributes(G,' pos')pos)

nx.write_graphml(G,g.graphml)

nx.draw_networkx(G,pos)
plt.savefig( g.pdf)

下面是我得到的错误,问题在于如何保存位置(graphml

  C:\Aaconda\python.exe C:/ Users / sturaroa / Documents / PycharmProjects /node_labeling_test.py 
Traceback(最近一次调用最后一次):
在< module>文件中,第11行的文件C:/Users/sturaroa/Documents/PycharmProjects/node_labeling_test.py
nx.write_graphml(g,g.graphml)
文件< string>,第2行,位于write_graphml
文件C:\Anconda\lib\site -packages\\\
etworkx\utils\decorators.py,第220行,在_open_file
result = func(* new_args,** kwargs)
文件C:\Anconda\lib\\ \\ site-packages\\\
etworkx\readwrite\graphml.py,第82行,在write_graphml
writer.add_graph_element(G)
文件C:\Anconda\lib\site- packages_\
etworkx\readwrite\graphml.py,第350行,在add_graph_element
self.add_nodes(G,graph_element)
文件C:\Anconda\lib\site-packages \\\
etworkx\readwrite\graphml.py,第307行,在add_nodes中
self.add_attributes(node,node_element,data,default)
文件C:\Anconda\lib \site-packages\\\
etworkx\readwrite\graphml.py,第300行,在add_attributes
scope = scope,default = default_value)
文件C :\Anconda\lib\site-packages\\\
etworkx\readwrite\graphml.py,第288行,将add_data
'%s作为数据值。'%element_type)
networkx .exception.NetworkXError:GraphML编写器不支持< type'numpy.ndarray'>作为数据值。

我的印象是,将位置定义为2个单独的节点属性会更好,x和y,并将它们分开保存,以graphml格式定义每个关键字,像这样



然而,我对Python并不熟悉,并且希望您在我进行混乱迭代之前先看到您的意见。



谢谢。

解决方案

你说得对,GraphML想要更简单的属性(没有numpy数组或列表)。

您可以将节点的x和y位置设置为这样的属性

  G = nx.path_graph(4)
pos = nx.spring_layout(G)

表示节点,(x,y)表示pos .items():
G.node [node] ['x'] = float(x)
G.node [node] ['y'] = float(y)

nx.write_graphml(G,g.graphml)


I'm using NetworkX 1.9.1.

I have a graph that I need to organize with positions and I then export to graphml format.

I've tried code in this question. It does not work, here is my example

import networkx as nx
import matplotlib.pyplot as plt

G = nx.read_graphml("colored.graphml")

pos=nx.spring_layout(G) # an example of quick positioning
nx.set_node_attributes(G, 'pos', pos)

nx.write_graphml(G, "g.graphml")

nx.draw_networkx(G, pos)
plt.savefig("g.pdf")

Here are the errors I get, the problem is how positions are saved (graphml does not accept arrays).

C:\Anaconda\python.exe C:/Users/sturaroa/Documents/PycharmProjects/node_labeling_test.py
Traceback (most recent call last):
  File "C:/Users/sturaroa/Documents/PycharmProjects/node_labeling_test.py", line 11, in <module>
    nx.write_graphml(G, "g.graphml")
  File "<string>", line 2, in write_graphml
  File "C:\Anaconda\lib\site-packages\networkx\utils\decorators.py", line 220, in _open_file
    result = func(*new_args, **kwargs)
  File "C:\Anaconda\lib\site-packages\networkx\readwrite\graphml.py", line 82, in write_graphml
    writer.add_graph_element(G)
  File "C:\Anaconda\lib\site-packages\networkx\readwrite\graphml.py", line 350, in add_graph_element
    self.add_nodes(G,graph_element)
  File "C:\Anaconda\lib\site-packages\networkx\readwrite\graphml.py", line 307, in add_nodes
    self.add_attributes("node", node_element, data, default)
  File "C:\Anaconda\lib\site-packages\networkx\readwrite\graphml.py", line 300, in add_attributes
    scope=scope, default=default_value)
  File "C:\Anaconda\lib\site-packages\networkx\readwrite\graphml.py", line 288, in add_data
    '%s as data values.'%element_type)
networkx.exception.NetworkXError: GraphML writer does not support <type 'numpy.ndarray'> as data values.

I'm under the impression that I would be better off defining positions as 2 separate node attributes, x and y, and save them separately, defining a key for each of them in the graphml format, like this.

However, I'm not that familiar with Python, and would like your opinion before I make a mess iterating back and forth.

Thanks.

解决方案

You are right, GraphML want's simpler attributes (no numpy arrays or lists).

You can set the x and y positions of the nodes as attributes like this

G = nx.path_graph(4)
pos = nx.spring_layout(G)

for node,(x,y) in pos.items():
    G.node[node]['x'] = float(x)
    G.node[node]['y'] = float(y)

nx.write_graphml(G, "g.graphml")

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

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