通过 graphviz 的 dot 绘图

发布时间: 更新时间: 总字数:382 阅读时间:1m 作者:IP:上海 网址

graphviz 是贝尔实验室开发的一款开源绘图工具软件,使用DSL(领域特定语言) dot语法 作为脚本语言,通过布局引擎来解析此脚本,完成自动布局绘图

介绍

布局引擎(Layout Engines):

  • dot 默认布局引擎(层次布局),主要用于有向图
  • neato 无向图布局,基于 spring 模型(force-based)算法
  • fdp 无向图布局,类似 neato
  • sfdp 适合大型无向图的多尺度布局
  • twopi 放射状(径向)布局
  • circo 环形布局

注:digraph 和 graph 是 DOT 语言中声明有向图和无向图的关键字,而不是布局引擎。

dot 语法说明:

  • -> 表示有向图中的边
  • -- 表示无向图中的边

安装

bash
apt install graphviz -y
  • 生成图片
text
dot digraph.dot -T png -o digraph.png

digraph 有向图

  • digraph.dot
text
digraph demo{
    a->b;
    b->d;
    c->d;
}

graph 无向图

  • graph.dot
text
graph demo1 {
    a -- b
    b -- c
    c -- a
}

负责的图

xml
digraph action {
    node [shape = record,height=.1];

    node0 [label = "<head> head|<body> body|<foot> foot", height=.5]
    node2 [shape = box label="mind"]

    node0:head:n -> node2:n [label = "n"]
    node0:head:ne -> node2:ne [label = "ne"]
    node0:head:e -> node2:e [label = "e"]
    node0:head:se -> node2:se [label = "se"]
    node0:head:s -> node2:s [label = "s"]
    node0:head:sw -> node2:sw [label = "sw"]
    node0:head:w -> node2:w [label = "w"]
    node0:head:nw -> node2:nw [label = "nw"]
    node0:head:c -> node2:c [label = "c"]
    node0:head:_ -> node2:_ [label = "_"]

    node0:body[style=filled color=lightblue]
}

扩展

  • python 库 pip install pydotplus