如何通过使用write_dot来绘制DiGraph的权重?

时间:2018-04-16 22:23:10

标签: python-3.x anaconda networkx

import networkx as nx
import matplotlib.pyplot as plt
from networkx.drawing.nx_agraph import write_dot
import pylab
lst = []
G = nx.DiGraph()
nodes = int(input("Enter a number of nodes: "))- 1
for i in range(nodes):
    G.add_node(nodes)
transitions=[]
for i in range(int(input("Enter a number of transitions: "))):
    frm = int(input("Enter from: "))
    to = int(input("Enter to:"))
    wght = int(input("Enter a w:"))
    transitions.append((frm,to,wght))
G.add_weighted_edges_from(transitions,edge_labels=True)
labels = nx.get_edge_attributes(G,'weight')
pos=nx.spring_layout(G)
nx.draw_networkx_edge_labels(G,pos,edge_labels=labels)
write_dot(G,'graph.dot')

我想创建类似于有限自动状态图的东西,但问题是我不能在图上表示重量。

1 个答案:

答案 0 :(得分:0)

解决方案是使用graphviz库而不是networkx     import graphviz