我正在尝试导出带有一些可视化规范的图形文件.我无法弄清楚如何添加多级属性.
Import networkx as nx
#Create the Graph
g = nx.Graph()
g.add_edge('Andre', 'Beverly')
g.add_edge('Andre', 'Diane')
g.add_edge('Andre', 'Carol')
g.add_edge('Andre', 'Fernando')
g.add_edge('Beverly', 'Diane')
nx.draw(g)
Run Code Online (Sandbox Code Playgroud)
我想做的是添加位置但使用特定属性(名称和结构)
# compute position
pos = nx.spring_layout(g)
# add attribute
g.node["Andre"]["viz"]["position"]["x"]= pos["Andre"][0]
g.node["Andre"]["viz"]["position"]["y"]= pos["Andre"][1]
Run Code Online (Sandbox Code Playgroud)
我其实有两个问题:
我正在使用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")
Run Code Online (Sandbox Code Playgroud)
以下是我得到的错误,问题是如何保存位置(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", …Run Code Online (Sandbox Code Playgroud)