推荐直接用graphviz画图。
networkx画图
备注:不推荐,复杂的图看起来有问题
安装networkx、matplotlib
画图
import networkx as nx
import matplotlib.pyplot as plt
G = nx.DiGraph() # 创建一个有向图
G.add_node("A") # 添加节点A
G.add_node("B") # 添加节点B
G.add_node("C") # 添加节点C
G.add_edge("A", "B") # 添加从A到B的边
G.add_edge("B", "C") # 添加从B到C的边
G.add_edge("C", "A") # 添加从C到A的边
nx.draw(G, with_labels=True)
plt.show()
运行
运行结果如下图所示:
graphviz画图
安装
brew install graphviz
pip install graphviz
画图
import graphviz
dot = graphviz.Digraph()
nodes = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
deps = [
('1', '2'),
('2', '3'),
('3', '4'),
('4', '5'),
('5', '6'),
('6', '7'),
('7', '8'),
('8', '9'),
('9', '1'),
]
for node in nodes:
dot.node(node, node)
for dep in deps:
dot.edge(dep[1], dep[0])
dot.render('dag', view=True)