属性#

示例说明在转换为/从 AGraph 过程中,节点、边和图的属性是如何处理的。

plot attributes
edges
[('1', '2', {'color': 'red'}), ('2', '3', {'color': 'red'})]
default graph attributes
{'graph': {}, 'node': {}, 'edge': {'color': ''}}
node node attributes
[('1', {}), ('2', {}), ('3', {}), ('4', {})]

import networkx as nx

G = nx.Graph()
G.add_edge(1, 2, color="red")
G.add_edge(2, 3, color="red")
G.add_node(3)
G.add_node(4)

A = nx.nx_agraph.to_agraph(G)  # 转换为Graphviz图
A.draw("attributes.png", prog="neato")  # Draw with pygraphviz

# 将带有边属性的图转换回networkx图
# 默认属性作为字典数据
X = nx.nx_agraph.from_agraph(A)
print("edges")
print(list(X.edges(data=True)))
print("default graph attributes")
print(X.graph)
print("node node attributes")
print(X.nodes.data(True))

Total running time of the script: (0 minutes 0.019 seconds)

Gallery generated by Sphinx-Gallery