因此,渲染图是graphviz 的独特之处,它也恰好有几个提供 python 绑定的库。在我看来,这些绑定库中最好的是pygraphviz。 Graphviz 可能是最好的解决方案,也可能是最简单的解决方案。
您在问题中描述的特定布局是一种分层的分层方案,由 graphviz 的 dot 布局引擎轻松执行。 Dot 执行渲染以确保图形以自然树配置布局 - 即,父节点位于其子节点之上;相同等级的节点(从根开始的级别)尽可能在 y 轴上等位,并尽可能保持自然对称性。
(注意:令人困惑的是,dot 是指组成 graphviz 的几个布局引擎之一,但 dot 也是名称和文件扩展名所有graphviz文档的文件格式,无论它们如何呈现)。
正如您在下面的代码中看到的那样,使用 pygraphviz,选择 dot 作为图形的布局引擎很简单,尽管它实际上不是默认设置 (neato是)。
这是我制作的快速图表,然后使用 dot 进行渲染——通过 pygraphviz 使用 graphviz 创建和渲染。
请注意,该图具有完美的布局——相同度数的节点沿垂直轴处于同一水平,子节点呈现在父节点下方,并且尽可能保留自然“对称性”(例如,父节点位于在它的两个子节点之上。正如你所看到的,我的代码都没有手动控制布局——graphviz,即dot,自动处理它。
import pygraphviz as PG
A = PG.AGraph(directed=True, strict=True)
A.add_edge("7th Edition", "32V")
A.add_edge("7th Edition", "Xenix")
# etc., etc.
# save the graph in dot format
A.write('ademo.dot')
# pygraphviz renders graphs in neato by default,
# so you need to specify dot as the layout engine
A.layout(prog='dot')
# opening the dot file in a text editor shows the graph's syntax:
digraph unix {
size="7,5";
node [color=goldenrod2, style=filled];
"7th Edition" -> "32V";
"7th Edition" -> "V7M";
"7th Edition" -> "Xenix";
"7th Edition" -> "UniPlus+";
"V7M" -> "Ultrix-11";
"8th Edition" -> "9th Edition";
"1 BSD" -> "2 BSD";
"2 BSD" -> "2.8 BSD";
"2.8 BSD" -> "Ultrix-11";
"2.8 BSD" -> "2.9 BSD";
"32V" -> "3 BSD";
"3 BSD" -> "4 BSD";
"4 BSD" -> "4.1 BSD";
"4.1 BSD" -> "4.2 BSD";
"4.1 BSD" -> "2.8 BSD";
"4.1 BSD" -> "8th Edition";
"4.2 BSD" -> "4.3 BSD";
"4.2 BSD" -> "Ultrix-32";
}