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

编程入门 行业动态 更新时间:2024-10-25 06:29:45
使用NetworkX将图形导出到带有节点位置的graphml(Export graph to graphml with node positions using NetworkX)

我正在使用NetworkX 1.9.1。

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

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

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")

以下是我得到的错误,问题是如何保存位置(graphml不接受数组)。

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.

我的印象是我最好将位置定义为2个单独的节点属性x和y,并单独保存它们,以graphml格式为每个属性定义一个键, 就像这样 。

但是,我对Python并不熟悉,并且在我来回迭代之前想要你的意见。

谢谢。

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.

最满意答案

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

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

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")

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")

更多推荐

本文发布于:2023-08-07 09:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1463812.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:节点   图形   位置   导出到   NetworkX

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!