本文最后更新于:2024年1月14日 晚上

Graphviz 是一个开源的图可视化工具,非常适合绘制结构化的图标和网络。 本文记录安装即使用方法。

简介

  • graphviz 是贝尔实验室设计的一个开源的画图工具,它的强大主要体现在“所思即所得"(WYTIWYG,what you think is what you get),这是和office的“所见即所得“(WYSIWYG,what you see is what you get)完全不同的一种方式。

  • 它的输入是一个用dot语言 编写的绘图脚本,通过对输入脚本的解析,分析出其中的点,边以及子图,然后根据属性进行绘制。

  • 用graphviz来绘图的时候,你的主要工作就是编写dot脚本,只要关注图中各个点之间的关系,不需要考虑如何安排各个节点的位置。

  • 官方示例:https://graphviz.org/gallery/

下载安装

Linux

Windows

  • 下载安装包 进行安装

  • 安装时建议勾选将 安装目录 /bin 添加至系统路径

    如果没有勾选,安装后需要手动将 安装目录 /bin 添加至系统路径

Mac

测试

  • 命令行执行
1
dot -V
  • 返回 graphviz 的版本信息则表示安装、路径配置完成
1
dot - graphviz version 4.0.0 (20220529.0937)

使用

布局引擎

  • graphviz中包含了众多的布局器:
布局方式 描述
dot 默认布局方式,主要用于有向图
neato 基于spring-model(又称force-based)算法
twopi 径向布局
circo 圆环布局
fdp 主要用于无向图
  • 分别对应 graphviz 安装目录下 bin 文件夹中的可执行程序

帮助

  • 执行 dot --help 可以查看帮助文档
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
$ dot --help
Error: dot: option -- unrecognized

Usage: dot [-Vv?] [-(GNE)name=val] [-(KTlso)<val>] <dot files>
(additional options for neato) [-x] [-n<v>]
(additional options for fdp) [-L(gO)] [-L(nUCT)<val>]
(additional options for memtest) [-m<v>]
(additional options for config) [-cv]

-V - Print version and exit
-v - Enable verbose mode
-Gname=val - Set graph attribute 'name' to 'val'
-Nname=val - Set node attribute 'name' to 'val'
-Ename=val - Set edge attribute 'name' to 'val'
-Tv - Set output format to 'v'
-Kv - Set layout engine to 'v' (overrides default based on command name)
-lv - Use external library 'v'
-ofile - Write output to 'file'
-O - Automatically generate an output filename based on the input filename with a .'format' appended. (Causes all -ofile options to be ignored.)
-P - Internally generate a graph of the current plugins.
-q[l] - Set level of message suppression (=1)
-s[v] - Scale input by 'v' (=72)
-y - Invert y coordinate in output

-n[v] - No layout mode 'v' (=1)
-x - Reduce graph

-Lg - Don't use grid
-LO - Use old attractive force
-Ln<i> - Set number of iterations to i
-LU<i> - Set unscaled factor to i
-LC<v> - Set overlap expansion factor to v
-LT[*]<v> - Set temperature (temperature factor) to v

-m - Memory test (Observe no growth with top. Kill when done.)
-m[v] - Memory test - v iterations.

-c - Configure plugins (Writes $prefix/lib/graphviz/config
with available plugin information. Needs write privilege.)
-? - Print usage and exit

基本使用

  • 所有的 graphviz 命令都使用相同的格式
1
cmd [ flags ] [ input files ]
命令参数 描述
cmd 指布局引擎,包括:dot,neato,twopi,circo,fdp
flags 指帮助文档中提供的标志配置方法,官方文档
input files 输入文件路径
  • 支持有向图 digraph 和无向图 graph

基本示例

  • 以一个精简版的官方示例为例
  • 图文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
digraph regexp { 
fontname="Helvetica,Arial,sans-serif"
node [fontname="Helvetica,Arial,sans-serif"]
edge [fontname="Helvetica,Arial,sans-serif"]
n0 -> n1;
n0 -> n2;
n0 -> n3;
n0 -> n4;
n0 -> n5;
n0 -> n6;
n0 -> n7;
n0 -> n8;
n0 -> n9;
n1 -> n10;
n1 -> n2;
n1 -> n8;
n1 -> n9;
n1 -> n11;
n2 -> n11;
n2 -> n7;
n3 -> n4;
n3 -> n5;
n3 -> n6;
n3 -> n8;
n3 -> n9;
n4 -> n12;
n5 -> n10;
n5 -> n13;
n5 -> n9;
n5 -> n11;
n5 -> n14;
n6 -> n2;
n6 -> n7;
n6 -> n15;
n6 -> n11;
n6 -> n10;
n6 -> n8;
n6 -> n9;
n7 -> n16;
n7 -> n17;
n7 -> n18;
n7 -> n15;
n10 -> n19;
n10 -> n15;
n11 -> n12;
n12 -> n17;
n12 -> n15;
n13 -> n15;
n13 -> n19;
n13 -> n14;
n14 -> n15;
n16 -> n15;
n18 -> n15;
}
  • 将该文件保存为 test.txt 文本文件
  • 在文件目录执行命令
1
dot -Tpng test.txt -o test.png
  • 生成 test.png 文件

参数配置

  • 可以配置图形属性,包括 graph , node, edge 三种属性

    node与edge公用样式:"dashed"虚线, "dotted"点, "solid"固体框, "invis"隐藏 and “bold” 加粗

  • graph 属性在配置文件中时可以不用强调 graph [] ,直接写入属性

命令行配置
  • 可以在命令行配置,如帮助文档中的使用方法:

    1
    2
    3
    -Gname=val  - Set graph attribute 'name' to 'val'
    -Nname=val - Set node attribute 'name' to 'val'
    -Ename=val - Set edge attribute 'name' to 'val'
    • 以上文示例为例,如需要通过配置 graph 属性为图形添加红色的标题,可以在命令行执行
    1
    dot -Tpng -Gfontcolor=red -Glabel="My favorite letters" test.txt -o test.png

图文件配置
  • 也可以在图的生成文件中配置属性
  • 以上文示例为例,如需要通过配置 graph 属性为图形添加红色的标题,并配置node 属性,可以修改配置文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
digraph regexp { 
fontname="Helvetica,Arial,sans-serif"
fontcolor="blue"
label="My favorite letters"
node [fontname="Helvetica,Arial,sans-serif", fontcolor="red"]
edge [fontname="Helvetica,Arial,sans-serif", color="red", arrowhead="diamond"]
n0 -> n1;
n0 -> n2;
n0 -> n3;
n0 -> n4;
n0 -> n5;
n0 -> n6;
n0 -> n7;
n0 -> n8;
n0 -> n9;
n1 -> n10;
n1 -> n2;
n1 -> n8;
n1 -> n9;
n1 -> n11;
n2 -> n11;
n2 -> n7;
n3 -> n4;
n3 -> n5;
n3 -> n6;
n3 -> n8;
n3 -> n9;
n4 -> n12;
n5 -> n10;
n5 -> n13;
n5 -> n9;
n5 -> n11;
n5 -> n14;
n6 -> n2;
n6 -> n7;
n6 -> n15;
n6 -> n11;
n6 -> n10;
n6 -> n8;
n6 -> n9;
n7 -> n16;
n7 -> n17;
n7 -> n18;
n7 -> n15;
n10 -> n19;
n10 -> n15;
n11 -> n12;
n12 -> n17;
n12 -> n15;
n13 -> n15;
n13 -> n19;
n13 -> n14;
n14 -> n15;
n16 -> n15;
n18 -> n15;
}
  • 直接生成图像,在命令行不需要添加属性
1
dot -Tpng test.txt -o test.png
  • 生成效果

节点属性
  • 属性可以设置在节点和边上,用一对 [] 表示,多个属性可以用空格或者 , 隔开。
1
2
3
4
5
6
7
8
9
strict graph {
// 设置节点属性
b [shape=box];
c [shape=triangle];

// 设置边属性
a -- b [color=blue];
a -- c [style=dotted];
}

分段

  • 边属性支持分段
1
2
3
4
digraph G {
a -> b [dir=both color="red:blue"]
c -> d [dir=none color="green:red;0.25:blue"]
}

python 调用

1
pip install graphviz
  • 使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 引入库
import graphviz
# 创建有向图,不同渲染引擎修改参数engine, e.g. engine='fdp'
dot = graphviz.Digraph(comment='The Round Table')
# 配置全局属性,以 graph 属性为例
# 可以使用弯曲的连接线
dot.graph_attr['splines'] = 'true'
# 禁止节点重叠
dot.graph_attr['overlap'] = 'false'
# 添加节点
dot.node('A', 'King Arthur')
dot.node('B', 'Sir Bedevere the Wise')
dot.node('L', 'Sir Lancelot the Brave')
# 添加 边
dot.edges(['AB', 'AL'])
dot.edge('B', 'L', constraint='false')
# 渲染
dot.render(view=False, cleanup=True)
  • 得到 pdf 文件

参考资料



文章链接:
https://www.zywvvd.com/notes/tools/graphviz/graphviz/


“觉得不错的话,给点打赏吧 ୧(๑•̀⌄•́๑)૭”

微信二维码

微信支付

支付宝二维码

支付宝支付

Graphviz 使用教程
https://www.zywvvd.com/notes/tools/graphviz/graphviz/
作者
Yiwei Zhang
发布于
2022年6月15日
许可协议