Graphviz 是一个开源的图可视化工具,非常适合绘制结构化的图标和网络。 本文记录安装即使用方法。
graphviz
是贝尔实验室设计的一个开源的画图工具,它的强大主要体现在“所思即所得"(WYTIWYG,what you think is what you get),这是和office的“所见即所得“(WYSIWYG,what you see is what you get)完全不同的一种方式。
sudo apt install graphviz
sudo yum install graphviz
sudo apt install graphviz
sudo yum install graphviz
安装目录 /bin
添加至系统路径
如果没有勾选,安装后需要手动将 安装目录 /bin
添加至系统路径
dot -V
dot - graphviz version 4.0.0 (20220529.0937)
布局方式 | 描述 |
---|---|
dot | 默认布局方式,主要用于有向图 |
neato | 基于spring-model(又称force-based)算法 |
twopi | 径向布局 |
circo | 圆环布局 |
fdp | 主要用于无向图 |
graphviz
安装目录下 bin
文件夹中的可执行程序dot --help
可以查看帮助文档$ 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
cmd [ flags ] [ input files ]
命令参数 | 描述 |
---|---|
cmd | 指布局引擎,包括:dot,neato,twopi,circo,fdp |
flags | 指帮助文档中提供的标志配置方法,官方文档 |
input files | 输入文件路径 |
digraph
和无向图 graph
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
文本文件dot -Tpng test.txt -o test.png
test.png
文件graph
, node
, edge
三种属性
node与edge公用样式:"dashed"虚线, "dotted"点, "solid"固体框, "invis"隐藏 and “bold” 加粗
graph
属性在配置文件中时可以不用强调 graph []
,直接写入属性
可以在命令行配置,如帮助文档中的使用方法:
-Gname=val - Set graph attribute 'name' to 'val'
-Nname=val - Set node attribute 'name' to 'val'
-Ename=val - Set edge attribute 'name' to 'val'
dot -Tpng -Gfontcolor=red -Glabel="My favorite letters" test.txt -o test.png
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;
}
dot -Tpng test.txt -o test.png
strict graph {
// 设置节点属性
b [shape=box];
c [shape=triangle];
// 设置边属性
a -- b [color=blue];
a -- c [style=dotted];
}
digraph G {
a -> b [dir=both color="red:blue"]
c -> d [dir=none color="green:red;0.25:blue"]
}
pip install graphviz
# 引入库
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)