我正在尝试使用Python的networkx创建有向图的图像,该图像显示自循环。 Networkx没有显示它们,但是其他在线论坛建议将networkx与graphviz进行接口。这是我输入的由五个节点组成的定向网络的内容,该网络正在Python中完成。
# Import packages
import networkx as nx
import numpy as npy
from networkx.drawing.nx_agraph import to_agraph
#Non-symmetric adjacency matrix with self-loop at 1.
A=npy.matrix([[1,1,1,0,0],[1,0,1,0,0],[1,1,0,1,1],[0,0,1,0,1],
[0,0,1,1,0]])
#Create directed graph
G=nx.DiGraph(A)
#add graphviz layout options (as per advice at
#https://stackoverflow.com/a/39662097)
G.graph['edge'] = {'arrowsize': '0.8', 'splines':
'curved','shape':'circle'}
G.graph['graph'] = {'scale': '3'}
G.graph['node']={'shape':circle','fillcolor':'red'}
D = to_agraph(G)
D.layout('dot')
D.draw('multi.png')
一切正常。但是,我想将存储在Python字典中的标签用作 标签= {0:'A',1:'B',2:'C',3:'D',4:'E'}
我不确定如何在graphviz布局选项中添加它。显然,这是用于简单图形的,但是我的字典标签通常更大。