Python,pygraphviz,networkx
作者:互联网
我使用networkx构建了一个有向加权图,我可以绘制它,但即使图形非常小,它也经常交叉边.我也使用pygraphviz,但我无法添加标签.有人可以帮助我吗?
edge_labels=dict([((u,v,),d['weight'])
for u,v,d in DG.edges(data=True)])
pylab.figure(1)
pos=nx.spring_layout(DG)
nx.draw(DG, pos)
nx.draw_networkx_edge_labels(DG,pos,edge_labels=result,font_size=10)
pylab.show()
如何将其转换为pygraphviz图并为其添加标签
解决方法:
Graphviz在边上绘制’label’属性.以下是将label属性设置为边权重的示例(如果存在).
import networkx as nx
import pygraphviz as pgv # need pygraphviz or pydot for nx.to_agraph()
G = nx.DiGraph()
G.add_edge(1,2,weight=7)
G.add_edge(2,3,weight=8)
G.add_edge(3,4,weight=1)
G.add_edge(4,1,weight=11)
G.add_edge(1,3)
G.add_edge(2,4)
for u,v,d in G.edges(data=True):
d['label'] = d.get('weight','')
A = nx.to_agraph(G)
A.layout(prog='dot')
A.draw('test.png')
标签:pygraphviz,python,networkx 来源: https://codeday.me/bug/20190830/1771721.html