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/
下载安装官方指引:https://www.graphviz.org/download/LinuxUbuntu packages代码语言:javascript复制sudo apt install graphviz
Fedora project代码语言:javascript复制sudo yum install graphviz
Debian packages代码语言:javascript复制sudo apt install graphviz
Stable and development rpms for Redhat Enterprise, or CentOS systems
可用但不是最新版本代码语言:javascript复制sudo yum install graphviz
Windows 下载安装包 进行安装
安装时建议勾选将 安装目录 /bin 添加至系统路径
如果没有勾选,安装后需要手动将 安装目录 /bin 添加至系统路径
Mac参考官网:https://www.graphviz.org/download/#mac测试命令行执行代码语言:javascript复制dot -V
返回 graphviz 的版本信息则表示安装、路径配置完成代码语言:javascript复制dot - graphviz version 4.0.0 (20220529.0937)
使用布局引擎graphviz中包含了众多的布局器:布局方式
描述
dot
默认布局方式,主要用于有向图
neato
基于spring-model(又称force-based)算法
twopi
径向布局
circo
圆环布局
fdp
主要用于无向图
分别对应 graphviz 安装目录下 bin 文件夹中的可执行程序帮助执行 dot --help 可以查看帮助文档代码语言:javascript复制$ dot --help
Error: dot: option -- unrecognized
Usage: dot [-Vv?] [-(GNE)name=val] [-(KTlso)
(additional options for neato) [-x] [-n
(additional options for fdp) [-L(gO)] [-L(nUCT)
(additional options for memtest) [-m
(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 - Set number of iterations to i
-LU - Set unscaled factor to i
-LC
-LT[*]
-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 命令都使用相同的格式代码语言:javascript复制cmd [ flags ] [ input files ]
命令参数
描述
cmd
指布局引擎,包括:dot,neato,twopi,circo,fdp
flags
指帮助文档中提供的标志配置方法,官方文档
input files
输入文件路径
支持有向图 digraph 和无向图 graph基本示例以一个精简版的官方示例为例图文件:代码语言:javascript复制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 文本文件在文件目录执行命令代码语言:javascript复制dot -Tpng test.txt -o test.png
生成 test.png 文件参数配置 可以配置图形属性,包括 graph , node, edge 三种属性
node与edge公用样式:"dashed"虚线, "dotted"点, "solid"固体框, "invis"隐藏 and “bold” 加粗
graph 属性在配置文件中时可以不用强调 graph [] ,直接写入属性
命令行配置可以在命令行配置,如帮助文档中的使用方法:
代码语言:javascript复制-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 属性为图形添加红色的标题,可以在命令行执行代码语言:javascript复制dot -Tpng -Gfontcolor=red -Glabel="My favorite letters" test.txt -o test.png
图文件配置也可以在图的生成文件中配置属性以上文示例为例,如需要通过配置 graph 属性为图形添加红色的标题,并配置node 属性,可以修改配置文件:代码语言:javascript复制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;
}
直接生成图像,在命令行不需要添加属性代码语言:javascript复制dot -Tpng test.txt -o test.png
生成效果节点属性属性可以设置在节点和边上,用一对 [] 表示,多个属性可以用空格或者 , 隔开。代码语言:javascript复制strict graph {
// 设置节点属性
b [shape=box];
c [shape=triangle];
// 设置边属性
a -- b [color=blue];
a -- c [style=dotted];
}
分段边属性支持分段代码语言:javascript复制digraph G {
a -> b [dir=both color="red:blue"]
c -> d [dir=none color="green:red;0.25:blue"]
}
python 调用 graphviz 支持 python 调用
pypi 主页:https://pypi.org/project/graphviz/
在安装好 graphviz 工具后需要安装 python 包
代码语言:javascript复制pip install graphviz
使用代码语言:javascript复制# 引入库
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://zhuanlan.zhihu.com/p/21993254https://blog.csdn.net/iamljj/article/details/5862930https://graphviz.org/gallery/https://www.jianshu.com/p/6d9bbbbf38b1https://pypi.org/project/graphviz/