Anaconda安装Graphviz, mac下Graphviz安装, pcharm中调用pycharm, Graphviz典型例子

 

 

 

 

mac下的Graphviz安装及使用

一.安装 
Graphviz http://www.graphviz.org/ 
mac用户建议直接用homebrew来安装,官网上版本比较旧php

1.安装homebrew 
打开终端复制、粘贴如下命令:html

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
  • 1

2.安装Graphviz 
homebrew安装完毕后运行 brew install graphviz便可java

注:运行第2步时可能会提示-bash: brew: command not found 
解决办法: 
终端输入sudo vim .bash_profile, 
输入i进入输入模式, 
添加export PATH="/usr/local/bin:$PATH" , 
按esc键退出输入模式, 
输入:wq退出并保存 
最后输入source .bash_profile使配置生效。 
(注意: 和空格也要输入,bash_profile保存时可能会警告,:w!强制保存便可,更多vim命令参考http://www.cnblogs.com/usergaojie/p/4583796.htmlnode

二.使用 
随便找的一个例子: 
1.使用文本编辑器(这里用的是sublime )输入如下内容:python

digraph pic { Hello -> World }
  • 1
  • 2
  • 3

2.保存为pic.dot文件 
3.打开终端,切换到dot文件所在目录,输入dot pic.dot -T png -o pic.png 
4.能够看到在dot文件目录下生成了一个pic.png文件 
这里写图片描述linux

三.进阶 
目的:流程图较复杂时,可以边写代码边看效果 
目前找了两个教程,还没试,有问题了再更新。 
https://blog.zengrong.net/post/2294.html 
https://zhuanlan.zhihu.com/p/22820399nginx

 

 

 

经过Anaconda安装Graphviz

简单三步走

1.打开Anaconda终端,Open terminal

image.png

2.在终端窗口一次输入:

conda install graphviz 
pip install graphviz 
image.pnggit

3.添加环境变量

找到Graphviz的安装路径,而后添加到环境变量中便可。个人安装路径是 
C:\Users\linxid\Anaconda3\Library\bin\graphvizgithub

安装后记得重启,环境变量修改才能够生效,调用库,便可成功。 
经过Graphviz绘制的决策树: 
image.pngredis

 

 

 

 

 

 
 
 
 
 
 
Graphviz Windows环境安装、配置环境变量、pcharm中调用、中文乱码、dot语句基本格式
 

Graphviz Windows环境安装:

        1.官网下载

           官网下载地址     

                

 

 

 

       2.建立桌面快捷方式

 

                安装目录\bin文件夹\:找到gvedit.exe文件右键 发送到桌面快捷方式,以下图:

 

 

 
        3.配置环境变量

 

                将graphviz安装目录下的bin文件夹添加到Path环境变量中:
 
 
        
        4.验证是否安装并配置成功
               进入windows命令行界面,输入dot -version,而后按回车,若是显示graphviz的相关版本信息,则安装配置成功。如图:
 
         5.python环境中安装:(pycharm中)
        
               
                而后输入Graphivz安装
 
        6.在pycharm中运行
            
# coding:utf-8
from graphviz import Digraph

dot = Digraph(comment='The Round Table')

# 添加圆点 A, A的标签是 King Arthur
dot.node('A', 'king')
dot.view()  #后面这句就注释了,也可使用这个命令查看效果

# 添加圆点 B, B的标签是 Sir Bedevere the Wise
dot.node('B', 'Sir Bedevere the Wise')
#dot.view()

# 添加圆点 L, L的标签是 Sir Lancelot the Brave
dot.node('L', 'Sir Lancelot the Brave')
#dot.view()

#建立一堆边,即链接AB的边,链接AL的边。
dot.edges(['AB', 'AL'])
#dot.view()

# 在建立两圆点之间建立一条边
dot.edge('B', 'L', constraint='false')
#dot.view()

# 获取DOT source源码的字符串形式
print(dot.source)

# 保存source到文件,并提供Graphviz引擎
dot.render('test-output/round-table.gv', view=True)
 
 
 
x
30
 
 
 
 
 
1
# coding:utf-8
2
from graphviz import Digraph
3
 
           
4
dot = Digraph(comment='The Round Table')
5
 
           
6
# 添加圆点 A, A的标签是 King Arthur
7
dot.node('A', 'king')
8
dot.view()  #后面这句就注释了,也可使用这个命令查看效果
9
 
           
10
# 添加圆点 B, B的标签是 Sir Bedevere the Wise
11
dot.node('B', 'Sir Bedevere the Wise')
12
#dot.view()
13
 
           
14
# 添加圆点 L, L的标签是 Sir Lancelot the Brave
15
dot.node('L', 'Sir Lancelot the Brave')
16
#dot.view()
17
 
           
18
#建立一堆边,即链接AB的边,链接AL的边。
19
dot.edges(['AB', 'AL'])
20
#dot.view()
21
 
           
22
# 在建立两圆点之间建立一条边
23
dot.edge('B', 'L', constraint='false')
24
#dot.view()
25
 
           
26
# 获取DOT source源码的字符串形式
27
print(dot.source)
28
 
           
29
# 保存source到文件,并提供Graphviz引擎
30
dot.render('test-output/round-table.gv', view=True)
 
 
结果:
        7. 中文乱码问题
      • 在安装目录找到以下文件用记事本打开:
         
 
      • 将对应位置代码修改成以下:
 
      • 代码中加以下语句:
  
       8.标签中有空格画出图出错的状况
                   将对应标签放在英文双引号内
 
       9.dot语言基本格式
      • dot input.dot -T png -o output.txt
            cmd:
    
                    -o    指定存储档案的档名
 
      • 有向图:diagraph申明,结点关系为"->",能够//注释
      • 无向图:graph 申明 结点关系"--"
      • 子图    :subgraph声明    父图是无向图他自己也得是无向图    父图是有向图自己也得是有向图
        10.示例
digraph G{
{ a b c} -> { d e f }
}
 
 
 
 
 
 
 
 
 
1
digraph G{
2
{ a b c} -> { d e f }
3
}
 
 
 
 
 
       
 
 
 
 
Graphviz(转载) 

简述

原文: http://www.tuicool.com/articles/vy2Ajyu

本文翻译自 Drawing Graphs using Dot and Graphviz

1. 许可

Copyright (C) 2013, 2014 Tony Ballantyne. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.

Code in this document is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This code is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

2. 介绍

2.1 什么是DOT?

DOT是纯文本图像描述语言,对于计算机和人来讲,都很容易阅读。

2.2 什么是Graphviz?

Graphviz是一个开源的图像可视化的软件,所谓图像可视化就是使用一种简单的方法,来将结构化的信息表现成用抽象图片和网络构成的示意图。

2.3 谁适合看这篇文章?

这篇文章原本是我写来给本身当速成手册看的。不过如今看起来已经被我弄成给学计算机的学生看的教程了。如今这篇文章能够给任何想经过例子学习DOT的人看。

2.4 相关材料

我博客还有相似的其余文章 TonyBallantyne.com/tech

若是你是想了解关于我做为一名SF and Fantasy writer的工做,但不幸点到这篇文章的话,下面的连接可能就对你比较有用:

译注:这儿所谓的相关材料实际上是做者本身其余的一些文章,跟DOT关系不大

3. 安装

若是你要顺着这个教程继续下去,那你可能就须要要装Graphviz套件了。Graphviz能够在 官网 免费下载。

4. 基础例子

4.1 简单图例

graph graphname { 
    a -- b; b -- c; b -- d; d -- a; }

4.2 同样的图,不同的布局

graph graphname {
    rankdir=LR;  //Rank Direction Left to Right
    a -- b; b -- c; b -- d; d -- a; }

4.3 简单有向图

digraph graphname{ a -> b; b -> c; a -> c; }

4.4 带标签的简单有向图

digraph graphname{

    T [label="Teacher"] // node T P [label="Pupil"] // node P T->P [label="Instructions", fontcolor=darkgreen] // edge T->P }

4.5 一样的图,不一样的形状和颜色

digraph graphname {
    T [label="Teacher" color=Blue, fontcolor=Red, fontsize=24, shape=box] // node T P [label="Pupil" color=Blue, fontcolor=Red, fontsize=24, shape=box] // node P T->P [label="Instructions", fontcolor=darkgreen] // edge T->P }

这儿你能够选择的形状有: box , polygon , ellipse , oval , circle , point , egg , triangle , plaintext , diamond , trapezium , parallelogram , house , pentagon , hexagon , septagon , octagon , doublecircle , doubleoctagon , tripleoctagon 更多的形状看 这里

4.6  总结

digraph summary{
    start [label="Start with a Node"] next [label="Choose your shape", shape=box] warning [label="Don't go overboard", color=Blue, fontcolor=Red,fontsize=24,style=filled, fillcolor=green,shape=octagon] end [label="Draw your graph!", shape=box, style=filled, fillcolor=yellow] start->next start->warning next->end [label="Getting Better...", fontcolor=darkblue] }

5. 高级

5.1 节省时间的技巧

单独地去定义每个节点其实很浪费时间的,下面这个技巧可以让你快点儿。

digraph hierarchy {

    nodesep=1.0 // increases the separation between nodes node [color=Red,fontname=Courier,shape=box] //All nodes will this shape and colour edge [color=Blue, style=dashed] //All the lines look like this Headteacher->{Deputy1 Deputy2 BusinessManager} Deputy1->{Teacher1 Teacher2} BusinessManager->ITManager {rank=same;ITManager Teacher1 Teacher2} // Put them on the same level }

5.2 记录

你如今能够用HTML来定义这一类节点了, 这里 有更多相关信息。

digraph structs {
    node[shape=record]
    struct1 [label="<f0> left|<f1> mid\ dle|<f2> right"]; struct2 [label="{<f0> one|<f1> two\n\n\n}" shape=Mrecord]; struct3 [label="hello\nworld |{ b |{c|<here> d|e}| f}| g | h"]; struct1:f1 -> struct2:f0; struct1:f0 -> struct3:f1; }

6. 例子

6.1 有限状态机

digraph finite_state_machine {
    rankdir=LR;
    size="8,5" node [shape = circle]; S0 -> S1 [ label = "Lift Nozzle" ] S1 -> S0 [ label = "Replace Nozzle" ] S1 -> S2 [ label = "Authorize Pump" ] S2 -> S0 [ label = "Replace Nozzle" ] S2 -> S3 [ label = "Pull Trigger" ] S3 -> S2 [ label = "Release Trigger" ] }

6.2 数据流示意图

digraph dfd{    
    node[shape=record]
    store1 [label="<f0> left|<f1> Some data store"]; proc1 [label="{<f0> 1.0|<f1> Some process here\n\n\n}" shape=Mrecord]; enti1 [label="Customer" shape=box]; store1:f1 -> proc1:f0; enti1-> proc1:f0; }

6.3 数据流示意图2

digraph dfd2{
    node[shape=record]
    subgraph level0{
    enti1 [label="Customer" shape=box]; enti2 [label="Manager" shape=box]; } subgraph cluster_level1{ label ="Level 1"; proc1 [label="{<f0> 1.0|<f1> One process here\n\n\n}" shape=Mrecord]; proc2 [label="{<f0> 2.0|<f1> Other process here\n\n\n}" shape=Mrecord]; store1 [label="<f0> |<f1> Data store one"]; store2 [label="<f0> |<f1> Data store two"]; {rank=same; store1, store2} } enti1 -> proc1 enti2 -> proc2 store1 -> proc1 store2 -> proc2 proc1 -> store2 store2 -> proc1 }

6.4 对象继承

digraph obj{
    node[shape=record];
    rankdir="BT"; teacher [label = "{<f0> Teacher|<f1> \n |<f2> \n }"]; course [label = "{<f0> Course|<f1> \n |<f2> \n }"]; student [label = "{<f0> Student|<f1> \n |<f2> \n }"]; lesson [label = "{<f0> Lesson |<f1> \n |<f2> \n }"]; tutorial [label = "{<f0> Tutorial|<f1> \n |<f2> \n }"]; assessment[label = "{<f0> Assessment|<f1> \n |<f2> \n }"]; coursework [label = "{<f0> Coursework|<f1> \n |<f2> \n }"]; exam [label = "{<f0> Exam|<f1> \n |<f2> \n }"]; {rank=same; teacher course student} teacher->course [dir="forward",arrowhead="none",arrowtail="normal",headlabel="1",taillabel="1.."]; student->course [dir="forward",arrowhead="none",arrowtail="normal",headlabel="1",taillabel="1.."]; lesson->course [dir="forward",arrowhead="diamond",arrowtail="normal"]; tutorial->course [dir="forward",arrowhead="diamond",arrowtail="normal"]; assessment->course [dir="forward",arrowhead="diamond",arrowtail="normal"]; coursework->assessment; exam->assessment; }

6.5 关系型实体

digraph ER{
    node[shape=box]; Book; Customer; Loan; {rank=same;Book,Customer,Loan} Book->Loan[dir="forward",arrowhead="crow",arrowtail="normal"]; Customer->Loan[dir="forward",arrowhead="crow",arrowtail="normal"]; }

7. 参考

如下多是你在画图时候最有用的一些属性,完整的列表能够在 这里 看。

7.1 图像属性

label="My Graph"; # 给图像设置标签 rankdir=LR; # 将图片由原来的从上到下布局变成从左到右布局 {rank=same; a, b, c } # 将一组元素放到同一个level splines="line"; # 让边框变为直线,没有曲线和锐角 K=0.6; # 用来在布局中影响spring属性,spring属性能够用于将节点往外推,这个在twopi和sfdp布局中颇有用。

译注:暂时还没明白这个spring属性应该怎么翻,初步猜想是弹性。胡克定律里面的常量名也叫K。

7.2 交点属性

[label="Some Label"] # 给交点打标签 [color="red"] # 给交点上色 [fillcolor="blue"] # 设置交点的填充色

7.3 边的属性

[label="Some Label"] # 给边设置标签 (设置路径权重的时候颇有用) [color="red"] # 给交点上色 (标示路径的时候颇有用) [penwidth=2.0] # 给边适配厚度,标示路径的时候颇有用。

7.4 尺寸, 背景颜色

fixedsize=true; size="1,1"; resolution=72; bgcolor="#C6CFD532"; # 不是我偷懒不翻译哦,原文就没有解释。

8. 附录

8.1 拓展阅读

An Introduction to GraphViz and dot

Graphviz Examples and Tutorial

8.2 使用Emacs的Org Mode

Emacs的Org Mode无论对于写做,仍是执行和导出DOT图片都是个很理想的工做环境。

8.2.1 配置

下载并安装Graphviz,而后把相关路径加到exec-path这个变量里去。

你也要把你的.emacs文件更新成可以把DOT做为babel语言加载,下面这个配置能够很容易的设置DOT为babel语言,其余语言也能够相似操做

(org-babel-do-load-languages (quote org-babel-load-languages) (quote ( (emacs-lisp . t) (java . t) (dot . t) (ditaa . t) (R . t) (python . t) (ruby . t) (gnuplot . t) (clojure . t) (sh . t) (ledger . t) (org . t) (plantuml . t) (latex . t) ) ) )

8.2.2 将Dot嵌入Emacs

Org Mode经过使用Library of Babel来解析各类语言。要想这么作的话,就用 begin_src 和 end_src 标签把你的dot代码想下面这样包含进去。你也须要在包裹的时候像下面那样添加一些命令行参数。

用 <s[TAB] 快捷键能够快速生成一个 begin_src 代码块。

#+begin_src dot :file ./img/example1.png :cmdline -Kdot -Tpng graph graphname { a -- b; b -- c; b -- d; d -- a; } #+end_src

8.2.3 命令行相关

#+begin_ src dot :file ./img/example1.png :cmdline -Kdot -Tpng 里的 :cmdline -Kdot -Tpng 就是命令行参数. 他们告诉dot如何渲染和展现。

  • -Kdot 使用 dot 布局方式. 你也能够尝试其余的布局方式,好比 Kneato , Kcirco , Ktwopi , Kfdp , Ksfdp
  • -Tpng 渲染成png格式

完整的命令行参数能够看 这里

Date: <2013-10-21 Mon>

Author: Tony Ballantyne

Translator: Casa Taloyum

Created: 2014-04-12 Sat 10:13

Emacs 23.3.1 (Org mode 8.0.2)

 

 

 

 

 

 

 

 

Graphviz典型例子

例子1

//先来看一个例子,咱们建立一个文档graph1.dot: //digraph是有向图,graph是无向图,要注意,->用在有向图中,--用在无向图中表示一条边,不能混用。 //第一行给出了图的类型和名字 digraph graph1{ main -> parse -> execute; main -> init; main -> cleanup; execute -> make_string; execute -> printf; init -> make_string; main -> printf; execute -> compare; }

效果:

01

例子2

/* 来看下一个稍微复杂点的例子,咱们开始手动的设置一下图的属性。能够给点设置属性, 也能够给边设置属性。先来说讲怎么设置边的属性,在每条边后面的双括号里设置边的属性。 也能够在用edge设置边的默认值。 而给点设置属性就必须给每一个点单独的设置一个属性,node表示点的默认值。 */ //点的默认参数是shape=ellipse, width=.75, height=.5 and labeled by the node name. //一些点的形状在appendix.h中,一些经常使用的形状有bos,circle,record,plaintext。 digraph graph2{ size = "4,4"; //把图的尺寸设为4 inch,4 inch main[shape=box]; //把main点的形状设为方形 main -> parse [weight=8]; //weight是设置了这条边的重要程度,默认是1 parse -> execute; main -> init [style=dotted] //让这条线是点状的 main -> cleanup; execute -> {make_string;printf} //这条语句一次链接了两条条线 init -> make_string; //把边的默认颜色设为了red edge[color=red]; //label就是在边上写了一行字 main -> printf[style=bold,label="100 times"]; //让make_string变成了一个两行的字符串(注意那个\n)。 make_string [label="make a\nstring"]; //设置了一下点的默认参数,蓝色,这个被用在了compare中。 node [shape=box,style=filled,color=".7 .3 1.0"]; execute -> compare; }

效果:

02

例子3

//能够设置每条边箭头的方向, //用dir,有forward(default),back,both,none四种。 digraph graph3{ A -> B[dir=both]; B -> C[dir=none]; C -> D[dir=back]; D -> A; }

效果:

例子4

/* 点的shape除了record和Mrecord这两种以外,其余的形状都是多边形, 而咱们能够对多边形进行一下属性上的设置,shape = polygon。 Sides用于设置它的边数,peripheries用于设置多边形的外框的层数, regular = true可让你的多边形是一个规则的多边形, orientation = *,可让你的多边形旋转一个角度, 如orientation = 15就是转了15度。 Skew后面跟一个(-1.0~1.0)的小数,能让你的图形斜切一个角度, distortion是让你的图形产生透视效果。 */ digraph graph4{ a -> b -> c; b -> d; a[shape=polygon,sides=5,peripheries=3.color=lightblue,style=filled]; c[shape=polygon,sides=4,skew=.4,label="hello world"]; d[shape=invtriangle]; e[shape=polygon,sides=4,distortion=.7]; }

效果:

例子5

//record和Mrecord的区别就是Mrecord的角是圆的。Record就是由矩形组成的图形。 digraph structs { node [shape=record]; struct1 [shape=record,label="<f0> left | <f1> middle | <f2> right"]; struct2 [shape=record,label="<f0> one | <f1> two"]; struct3 [shape=record,label="hello\nworld |{ b |{ c | <here> d | e }| f }| g | h"]; struct1 -> struct2; struct1 -> struct3; }

效果:

例子6

/* 当你的线和线label比较多时,能够给线的属性decorate = true, 使得每条线的label与所属线之间连线。你还能够给每条线加上headlabel和taillabel, 给每条线的起始点和终点加上label,他们的颜色由labelfontcolor来决定, 而label的颜色由fontcolor来决定。 */ graph graph6{ label = "I love you"; //给这幅图设置,名字 labelloc = b; //图名字的位置在bottom,也能够是t labeljust = l; //图名字的对齐方式在left,也能够是r edge[decorate = true]; C -- D[label = "s1"]; C -- E[label = "s2"]; C -- F[label = "s3"]; D -- E[label = "s4"]; D -- F[label = "s5"]; edge[decorate = false, labelfontcolor = blue, fontcolor = red]; C1 -- D1[headlabel = "c1", taillabel = "d1", label = "c1 - d1"]; }

效果:

例子7

digraph hierarchy {

    graph [fontname="Microsoft Yahei"]; nodesep=1.0 // increases the separation between nodes bgcolor="#EEE8D5" //All nodes will this shape and colour node [color="#FB9400",fontname="Microsoft Yahei",shape=box] edge [color="#414141", style=filled] //All the lines look like this rankdir = TB; n元线性方程组 -> {n维向量空间 矩阵} 双线性函数 -> 通常线性空间[dir=back] 通常线性空间 -> 线性映射[dir=foreward] {rank=same;双线性函数;通常线性空间;线性映射;} n维向量空间 -> 通常线性空间 矩阵 -> 线性映射[dir=both] }

效果:

例子8

digraph html {
   abc [shape=none, margin=0, label=< <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4"> <TR><TD>0</TD><TD>1</TD><TD>2</TD><TD>3</TD><TD>4</TD> </TR> <TR><TD>1</TD><TD></TD><TD></TD><TD></TD><TD></TD> </TR> <TR><TD>2</TD><TD></TD><TD></TD><TD></TD><TD></TD> </TR> <TR><TD>3</TD><TD></TD><TD></TD><TD></TD><TD></TD> </TR> <TR><TD>4</TD><TD></TD><TD></TD><TD></TD><TD></TD> </TR> </TABLE>>]; }

效果:

例子9

/* 默认时图中的线都是从上到下的,咱们能够将其改成从左到右, 在文件的最上层打入rankdir=LR就是从左到右,默认是TB(top -> bottom),也能够是RL,BT。 //当图中时间表之类的东西时,咱们会须要点能排在一行(列), 这时要用到rank,用花括号把rank=same,而后把须要并排的点一次输入。 */ digraph html { rankdir = LR; { node[shape = plaintext]; 1995 -> 1996 -> 1997 -> 1998 -> 1999 -> 2000 -> 2001; } { node[shape = box, style = filled]; WAR3 -> Xhero -> Footman -> DOTA; WAR3 -> Battleship; } {rank = same; 1996; WAR3;} {rank = same; 1998; Xhero; Battleship;} {rank = same; 1999; Footman;} {rank = same; 2001; DOTA;} }

效果:

09

/* 设立一条边时,咱们能够制定这条边从起点的那个位置射出和从哪一个位置结束。 控制符有"n", "ne","e", "se", "s", "sw", "w" 和 "nw",具体效果见下: */ digraph html { node[shape = box]; c:n -> d[label = n]; c1:ne -> d1[label = ne]; c2:e -> d2[label = e]; b:se -> a[label = se]; c3:s -> d3[label = s]; c4:sw -> d4[label = sw]; c5:w -> d5[label = w]; c6:nw -> d6[label = nw]; }

效果:

参考

Node, Edge and Graph Attributes

Gallery

Documentation

An Introduction to GraphViz and dot

Graphviz Examples and Tutorial

 

 
 
 
 
 
 
 
 
 
 
 
 

使用Graphviz绘图(一)

 

前言

平常开发或者编写课程论文时,除了代码文档的编写,其中一个很重要的部分就是须要绘制流程图、示意图

绘制流程图有不少工具,通常常见的就有以下几种:

  • Word、PPT等办公软件
  • Viso以及开源的Dia
  • 画图(MSPaint)、PS、AI
  • PicPicke
  • 在线流程图绘制(eg. www.processon.com)

对于这些软件不管功能强大与否,适合与否,方便与否,都具备一个特色——所见即所得。你制做过程当中看到的就是最终获得的结果。图中形式、布局在制做过程当中都由本身设置,其实还有一类绘图系统的存在,其思想是——所思即所得。其中具备表明性的软件就是——Graphviz。Graphviz是贝尔实验室开发的一个开源的工具包,它使用一个特定的DSL(领域特定语言)——Dot做为脚本语言,而后使用布局引擎解析脚本并完成自动布局。Graphviz的设计初衷是对图进行自动布局(有向图、无向图、),可使用dot脚原本定义图形元素,选择必定的算法进行布局,经过对输入脚本的解析,分析出其中的点,边以及子图,而后根据属性进行绘制,继而将结果以本身须要的格式导出来。

相对于其余的绘图软件,Granphviz的特色有以下几个方面:

  • 代码控制,所思即所得
  • 布局引擎自动布局
  • (导出格式很是丰富)

以下即为Graphivz官网上的一些示例效果:

一个生成有向图的小例子

咱们先来看一个小例子来看一下使用Graphviz绘制一张图的完整流程(我使用的是Ubuntu 14.04 LTS,在其它系统上的使用应该差很少,下面再也不标注了)。

要使用Graphviz,先要在系统上安装Graphviz。在Ubuntu上可使用命令sudo apt-get install graphviz进行安装,在其余系统安装的方法能够查看Graphviz官网进行查看。程序安装好以后咱们就可已使用了。

Step 1:首先,须要编辑dot脚本

可使用你熟悉的纯文本编辑器进行脚本编写(必须是纯文本编辑器,如vim、notepad++,像word这样的富文本编辑器是不行的),只需设置编码为UTF-8。

编辑下面的脚本代码,保存为test.dot(先不用管其具体的意思,直接复制就好了):

digraph G{ main -> parse -> execute; main -> init; main -> cleanup; execute -> make_string; execute -> printf; init -> make_string; main -> printf; execute -> compare; }

Step 2:随后,选用布局生成结果

使用以下命令生成结果:
dot -Tpng sample.dot -o sample.png

对于这条命令,dot表示用dot布局,-Tpng表示生成png图片格式,sample.dot脚本文件名-o sample.png表示生成输出的图片名称

改命令也能够写成dot -Kdot -Tpng sample.dot -o sample.png,其中-Kdot表示使用dot布局。

Graphviz支持几种布局引擎:

  • dot : 默认布局方式,主要用于有向图
  • neato : 主要用于无向图
  • twopi : 主要用于径向布局
  • circo : 圆环布局
  • fdp : 主要用于无向图
  • sfdp : 主要绘制较大的无向图
  • patchwork : 主要用于树哈希图(tree map)

Graphviz支持的输出图片格式更是至关的多,经常使用的有如下几种:

  • pdf :
  • gif
  • png :
  • jpeg : 一种有损压缩图片格式
  • bmp : 一种位图格式
  • svg : 矢量图,通常用与Web,,能够用浏览器打开
  • ps : 矢量线图,多用于打印
    更多的输出格式能够浏览Graphviz输出格式进行查看。

Step 3:查看生成结果

输出的图片,能够用支持相应图片格式的软件打开。Graphviz软件安装好以后,有一个图片浏览器能够进行图片预览,只需输入命令display sample.png便可(sample.png为生成的图片文件名),该示例预览结果以下(你能够在上一步使用不一样的布局方式,查看一下结果有什么不一样):

正确完成三个步骤获得结果说明Graphviz已经能够在你的系统中正确安装可使用了。后续我会介绍Graphviz Dot脚本的具体编写方法。

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

graphviz 安装和入门

2017-11-21   Humen_
 
 
 

画流程图装逼神器:graphviz,不解释

———————————以下言归正传,切入正题———————————–

一、下载

http://www.graphviz.org/Download_windows.php 
下载 graphviz-2.38.zip ,例如我解压到位置 D:\Program Files\graphviz

二、配置环境变量

将 D:\Program Files\graphviz\release\bin 添加到环境变量 Path 中。

三、验证

进入windows命令行界面,输入dot -version,而后按回车,若是显示graphviz的相关版本信息,则安装配置成功。 
以下:

C:\Users\shanhy>dot -version
dot - graphviz version 2.38.0 (20140413.2041)
libdir = "D:\Program Files\graphviz\release\bin"
Activated plugin library: gvplugin_dot_layout.dll
Using layout: dot:dot_layout
Activated plugin library: gvplugin_core.dll
Using render: dot:core
Using device: dot:dot:core
The plugin configuration file:
        D:\Program Files\graphviz\release\bin\config6
                was successfully loaded.
    render      :  cairo dot fig gd gdiplus map pic pov ps svg tk vml vrml xdot
    layout      :  circo dot fdp neato nop nop1 nop2 osage patchwork sfdp twopi
    textlayout  :  textlayout
    device      :  bmp canon cmap cmapx cmapx_np dot emf emfplus eps fig gd gd2 gif gv imap imap_np ismap jpe jpeg jpg m
etafile pdf pic plain plain-ext png pov ps ps2 svg svgz tif tiff tk vml vmlz vrml wbmp xdot xdot1.2 xdot1.4
    loadimage   :  (lib) bmp eps gd gd2 gif jpe jpeg jpg png ps svg
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

四、基本绘图入门

运行 D:\Program Files\graphviz\release\bin 目录下的 gvedit.exe 能够运行打开图形界面。 
File > New 建立新的脚本文件,编写脚本后,点击 “Layout”图标(快捷键F5)能够直接查看结果。

小试牛刀:

digraph gvDemo {
    main -> parse -> execute;
    main -> init;
    main -> cleanup;
    execute -> make_string;
    execute -> printf
    init -> make_string;
    main -> printf;
    execute -> compare;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

效果: 
这里写图片描述

digraph gvDemo{
    node [peripheries=2 style=filled color="#eecc80"]
    edge [color="sienna" fontcolor="green"]
    main -> parse -> execute;
    main -> init [arrowhead = box];
    main -> cleanupi -> main;
    make_string[label = once shape=parallelogram style=filled ]
    execute -> make_string[label=Go style=dashed arrowtail=diamond];
    execute -> printf [shape=box];
    init -> make_string;
    main -> printf[dir=none];
    execute -> compare[dir=both];
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

效果(这个不错,能够当之后的模板用 ^_^): 
这里写图片描述

示例1:graph 使用 – 描述关系

graph gvDemo1 {
    a -- b
    a -- b
    b -- a [color=blue]
}
  • 1
  • 2
  • 3
  • 4
  • 5

效果: 
这里写图片描述

示例2:digraph 使用 -> 描述关系

digraph gvDemo2 {
    a -> b
    a -> b
    b -> a [color=blue style=filled]
}
  • 1
  • 2
  • 3
  • 4
  • 5

效果: 
这里写图片描述

示例3:

digraph gvDemo3 {
    edge[fontname="Microsoft YaHei"]
    node[fontname="Microsoft YaHei"]
    graph[fontname="Microsoft YaHei"]
    label="游戏资源更新流程"
    rankdir="TB"
    start[label="启动游戏" shape=circle style=filled]
    ifwifi[label="网络环境判断是否 WIFI" shape=diamond]
    needupdate[label="是否有资源须要更新" shape=diamond]
    startslientdl[label="静默下载" shape=box]
    enterhall[label="进入游戏大厅" shape=box]


    enterroom[label="进入房间" shape=box]
    resourceuptodate[label="资源不完整" shape=diamond]
    startplay[label="正常游戏" shape=circle fillcolor=blue]
    warning[label="提醒玩家是否更新" shape=diamond]
    startdl[label="进入下载界面" shape=box]
    //{rank=same; needupdate, enterhall}


    {shape=diamond; ifwifi, needupdate}


    start -> ifwifi
    ifwifi->needupdate[label="是"]
    ifwifi->enterhall[label="否"]
    needupdate->startslientdl[label="是"]
    startslientdl->enterhall
    needupdate->enterhall[label="否"]


    enterhall -> enterroom
    enterroom -> resourceuptodate
    resourceuptodate -> warning[label="是"]
    resourceuptodate -> startplay[label="否"]
    warning -> startdl[label="确认下载"]
    warning -> enterhall[label="取消下载"]
    startdl -> enterhall[label="取消下载"]
    startdl -> startplay[label="下载完成"]
}
  • 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

效果: 
这里写图片描述

示例4:

graph gvDemo4{
    "黑海" -- "亚速海";
    "黑海" -- "博斯普鲁斯海峡"
    "达达尼尔海峡" -- "爱琴海"
    subgraph cluster_T{//新东西
        label = "黑海海峡";//新东西
        "达达尼尔海峡" -- "马尔马拉海" -- "博斯普鲁斯海峡";
    }
    subgraph cluster_M{
        label = "地中海海域";
        "中部地中海" -- {"爱琴海" "爱奥尼亚海" "西西里海峡"}; //也是新东西
        "西部地中海" -- {"西西里海峡" "第勒尼安海" "利古里亚海" "伊比利海" "阿尔沃兰海"};
        "爱奥尼亚海" -- "亚得里亚海";
        "阿尔沃兰海" -- "直布罗陀海峡";
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

效果: 
这里写图片描述

这张图有些新东西能够看。 
第一个是subgraph 关键字。一如名字所示,他是用来定义「次级图片」用的。 
次级图片在dot的官方文件中常被叫做cluster subgraph,特指图示中被方框包裹起来的那两块,其定义方式和通常的graph很是类似,不过使用上有两件事须要留意: 
graph的命名得以cluster前缀开头,不然语法虽然能过关,但生不出图面上您预期的效果。 
若是父图是无向图,他自己也得是无向图;反之若是父图是有向图,这边也得乖乖照着来。 
第二个重点是下面这段:

“中部地中海” – {“爱琴海” “爱奥尼亚海” “西西里海峡”}; 
用大括号括起,用空格分开-这是一口气将好几个节点群组起来同时操做的方法,其等效于: 
“中部地中海” – “爱琴海”; 2 “中部地中海” – “爱奥尼亚海”; 3 “中部地中海” – “西西里海峡”;

您甚至能够用如下程式码画出下面这个图:

digraph gvABC {
    { a b c } -> { d e f }
}
  • 1
  • 2
  • 3

这里写图片描述

除了直接使用工具查看和生成结果外,还可使用命令来操做:

语法:
<cmd> <inputfile> -T <format> -o <outputfile>
示例:
dot D:\test\1.gv -Tpng -o image.png
  • 1
  • 2
  • 3
  • 4

Graphviz 中的 cmd 有好多种,每种使用方法都彻底相同,差异只在于渲染出来的图片效果不同。 
dot 渲染的图具备明确方向性(最经常使用,通常都使用这个)。 
neato 渲染的图缺少方向性。 
twopi 渲染的图采用放射性布局。 
circo 渲染的图采用环型布局。 
fdp 渲染的图缺少方向性。 
sfdp 渲染大型的图,图片缺少方向性。

PS:

Setting(快捷键Shift + F5) 里面咱们能够设置生成的文件格式,例如能够生成 svg、pdf 等等格式,很强大。你能够参考工具里面能够选择的值带入到命令行执行。 
Graphviz 使用空格来解析脚本,因此咱们在编写脚本的时候,对于含有空格的字符,要使用双引号,这样才不会出现错误。 
更多关于 dot 语法和graphviz 的使用详见官方文档: http://www.graphviz.org/Documentation.php

关于中文乱码问题的解决方法: 先将源文件保存为UTF-8格式,而后对中文内容设置 frontname,例如:start[label="启动游戏" shape=circle style=filled, fontname="NSimSun"] 
或者设置全局属性,例如上面gvDemo3 中的代码片断:

digraph gvDemo3 {
    edge[fontname="Microsoft YaHei"]
    node[fontname="Microsoft YaHei"]
    graph[fontname="Microsoft YaHei"]

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

fontname后面也能够直接指定 xxx.ttf 字体文件 
Windows系统中文字体的英文名对应关系参考以下: 
新細明體:PMingLiU 
細明體:MingLiU 
標楷體:DFKai-SB 
黑体:SimHei 
宋体:SimSun 
新宋体:NSimSun 
仿宋:FangSong 
楷体:KaiTi 
仿宋_GB2312:FangSong_GB2312 
楷体_GB2312:KaiTi_GB2312 
微軟正黑體:Microsoft JhengHei 
微软雅黑体:Microsoft YaHei

参考资料: 
http://www.graphviz.org/Documentation/dotguide.pdf(官方) 
http://blog.jobbole.com/94472/

 
 
 
 
 
 

windows下Graphviz安装及入门教程

  • 下载安装配置环境变量
    • intall
    • 配置环境变量
    • 验证
  • 基本绘图入门
    • graph
    • digraph
    • 一个复杂的例子
  • 和python交互

 

发现好的工具,如同发现新大陆。有时,咱们会好奇,论文中、各类专业的书中那么形象的插图是如何作出来的,无一例外不是对绘图工具的熟练使用。


下载安装、配置环境变量

intall

windows版本下载地址:http://www.graphviz.org/download/

这里写图片描述

双击msi文件,而后一直next(记住安装路径,后面配置环境变量会用到路径信息),安装完成以后,会在windows开始菜单建立快捷信息,默认快捷方式不放在桌面。 


这里写图片描述 

 

配置环境变量

将graphviz安装目录下的bin文件夹添加到Path环境变量中:

 


这里写图片描述 

 

 


这里写图片描述 

 

验证

进入windows命令行界面,输入dot -version,而后按回车,若是显示graphviz的相关版本信息,则安装配置成功。

 


这里写图片描述 

 

基本绘图入门

打开windows下的graphviz编辑器gvedit,编写以下的dot脚本语言,保存成gv格式文本文件。而后进入命令行界面,使用dot命令,将gv文件转化为png图形文件。

dot D:\test\1.gv -Tpng -o image.png
  • 1

graph

graph使用--描述关系

graph pic1 { 
  a -- b a -- b b -- a [color=blue] } 

 

 


这里写图片描述 

 

digraph

使用->描述关系

digraph pic2 { a -> b a -> b b -> a [style=filled color=blue] } 

 

 


这里写图片描述 

 

一个复杂的例子

digraph startgame { label="游戏资源更新流程" rankdir="TB" start[label="启动游戏" shape=circle style=filled] ifwifi[label="网络环境判断是否 WIFI" shape=diamond] needupdate[label="是否有资源须要更新" shape=diamond] startslientdl[label="静默下载" shape=box] enterhall[label="进入游戏大厅" shape=box] enterroom[label="进入房间" shape=box] resourceuptodate[label="资源不完整" shape=diamond] startplay[label="正常游戏" shape=circle fillcolor=blue] warning[label="提醒玩家是否更新" shape=diamond] startdl[label="进入下载界面" shape=box] //{rank=same; needupdate, enterhall} {shape=diamond; ifwifi, needupdate} start -> ifwifi ifwifi->needupdate[label="是"] ifwifi->enterhall[label="否"] needupdate->startslientdl[label="是"] startslientdl->enterhall needupdate->enterhall[label="否"] enterhall -> enterroom enterroom -> resourceuptodate resourceuptodate -> warning[label="是"] resourceuptodate -> startplay[label="否"] warning -> startdl[label="确认下载"] warning -> enterhall[label="取消下载"] startdl -> enterhall[label="取消下载"] startdl -> startplay[label="下载完成"] }

 

 

 


这里写图片描述 

 

和python交互

graphviz强大而便捷的关系图/流程图绘制方法,很容易让咱们联想到机器学习中的Decision Tree的展现方式。幸运的是,scikit-learn提供了生成.dot文件的接口,具体操做以下:

python编辑环境下:

from sklearn.tree import export_graphviz # 导入的是一个函数 # tree表示已经训练好的模型,即已经调用过DecisionTreeClassifier实例的fit(X_train, y_train)方法 export_graphviz(tree, out_file='tree.dot', feature_names=['petal length', 'petal width'])

进入windows命令行界面,cd 切换到tree.dot所在的路径,执行

dot -Tpng tree.dot -o tree.png

 


这里写图片描述 

 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

学习Graphviz绘图

1、关于Graphviz

1.1 简介

Graphviz (英文:Graph Visualization Software的缩写)是一个由AT&T实验室启动的开源工具包,用于绘制DOT语言脚本描述的图形。它也提供了供其它软件使用的库。Graphviz是一个自由软件,其受权为Eclipse Public License。其Mac版本曾经得到2004年的苹果设计奖[2]。

Graphviz由一种被称为DOT语言的图形描述语言[3] 与一组能够生成和/或处理DOT文件的工具组成:

命令 说明
dot 一个用来将生成的图形转换成多种输出格式的命令行工具。其输出格式包括PostScript,PDF,SVG,PNG,含注解的文本等等。
neato 用于sprint model的生成(在Mac OS版本中称为energy minimized)。
twopi 用于放射状图形的生成
circo 用于圆形图形的生成。
fdp 另外一个用于生成无向图的工具。
dotty 一个用于可视化与修改图形的图形用户界面程序。
lefty 一个可编程的(使用一种被EZ影响的语言[4])控件,它能够显示DOT图形,并容许用户用鼠标在图上执行操做。Lefty能够做为MVC模型的使用图形的GUI程序中的视图部分。
  • 官网:http://www.graphviz.org/
  • 官方文档:http://www.graphviz.org/Documentation.php
  • 下载地址:http://www.graphviz.org/Download..php

1.2 DOT语言

DOT语言是一种文本图形描述语言它提供了一种简单的描述图形的方法,而且能够为人类和计算机程序所理解。DOT语言文件一般是具备.gv或是.dot的文件扩展名。不少程序均可以处理DOT文件。

1.3 安装

Mac下直接经过brew install graphviz了,也能够直接从官网下载。

1.4 简单示例

Graphviz画图只须要两步。第一步建立文本文件并命令为x.dot,输入如下内容:

graph demo {
    "Browser" -- {"Chrome", "Fiefox", "Safari", "..."}
}

第二步,使用命令将文本内容转换为图片。

dot demo.dot -T png -o demo.png

-T表示输出类型,能够指定jpg、gif、svg等等,-o指定输出文件名,不指定则输出到标准输出上。执行上面命令后不出意外则能够看到以下效果。

上面是用dot命令来生产,也能够用Graphviz中包含的其余命令来处理dot文本,好比用下面命令渲染出来的图片排列方式则不同。

neato demo.dot -T png -o demo.png

本文后面的示例都以dot命令来渲染。

1.5 开发工具

能够直接经过上面的这种命令生成,缺点就是看不到实时效果。有一些工具能够实时查看,我这里使用的是vscodePlantUML插件。AtomSublime应该也都有本身的插件,Eclipse上也能够安装PlantUML。Plantuml支持dot须要将开头的@startuml和结尾的@enduml换成@startdot@enddot,也能够在前面加个//或者#(#@startdot #@enddot),这样子dot命令生成的时候也不须要额外注释掉该部分。

2、DOT用法

2.1 基本用法

DOT中使用图(digraph/graph/subgraph)、节点(node)和边(edge)来描述关系图/流程图,在配合一些属性的设置完成绘图。看下面例子:

#@startdot

digraph demo {
    label="示例"
    bgcolor="beige"

    node[color="grey"]

    father[label="爸爸", shape="box"]
    mother[label="妈妈", shape="box"]
    brother[label="哥哥", shape="circle"]
    sister[label="姐姐", shape="circle"]
    node[color="#FF6347"]
    strangers[label="路人"]

    edge[color="#FF6347"]

    father->mother[label="夫妻", dir="both"]
    father->brother[label="父子"]
    father->sister[label="父子"]
    father->我[label="父子"]

    mother->{brother,sister,我}[label="母子"]

    {rank=same; father, mother}
    {rank=same; brother,sister,我}
}

#@enddot

说明:

  • graph 用来描述无向图,关系使用 --来描述,digraph用来描述有向图,关系使用 -> 来描述。
  • demo为定义的图片名称,label和bgcolor为定义的图片属性。
  • fathermotherbrothersisterstrangers为节点。能够把前面4个节点理解成变量定义,好比定义了一个变量father,名字叫爸爸,用方形来渲染,后面全部调用father都会按照方形爸爸来渲染,但这里其实和变量定义是不一样的,由于这个节点即使不用也会渲染出来,好比strangers,变量思惟为增强理解。好比没有定义也是能够直接使用的。
  • 描述节点与节点的关系理解为边。边有有向(->)和无向(--)两种。好比fathermother之间的关系为相互的。
  • 上面有两个特殊的节点,nodeedge,node用来定义节点的默认属性,edge用来定义边的默认属性。做用域从本次定义到下一次定义截住。特定节点/边设置的属性会覆盖默认值。
  • []内属性,属性能够针对图、节点、边来设置。
  • father 与子女的关系为一条条写的, mother 的关系则为直接经过大括号的方式来对应三个节点。
  • rank 定义设置节点处在同一行,辅助渲染出来的图的效果。
  • 注释和C语言相似,// 和 # 注释单行, /* */多行注释。
  • DOT的写法不算严格,好比结束能够有分号,属性能够没有引号。

最后看一下生成的效果图:

该图最基本的关系用一句话就能够表达出来了

{"爸爸", "妈妈"}->{"哥哥", "我", "姐姐"}

2.2 经常使用属性

2.2.1 经常使用图属性

属性名 默认值 说明
label   图片标签,如上面示例
bgcolor   背景颜色,颜色文档点此
fontcolor black 字体颜色,定义上面示例的颜色
fontname Times-Roman 字体
fontsize 14 字体大小
rank   子图等级限制, same,min,max,source,sink
rankdir TB 排序方向,LR(left to right) or TB(top to bottom)
compound false If true, allow edges between clusters. 配合 lhead 和 ltail 使用

2.2.2 经常使用节点属性

属性名 默认值 说明
label node name 节点显示内容
color black node边框颜色
fontcolor black 字体颜色
fillcolor   背景色
fontname Times-Roman 字体
fontsize 14 字体大小
shape ellipse 形状,box、ellipse、circle、diamond、plaintext、point、triangle、invtriangle
style   图形样式,eg. bold、dashed、dotted、filled
image   背景图片地址

shape示例

digraph demo {
    bgcolor="floralwhite"
    "box"[shape=box]
    "polygon"[shape=polygon,sides=7] 
    "ellipse"[shape=ellipse]
    "circle"[shape=circle]
    "point"[shape=point]
    "triangle"[shape=triangle]
    "invtriangle"[shape=invtriangle]
    "plaintext"[shape=plaintext]
    "diamond"[shape=diamond]
}

2.2.3 经常使用边属性

属性名 默认值 说明
label   描述关系
color black 箭头颜色
fontcolor black 关系文字颜色
dir forward 设置方向:forward,back,both,none
arrowhead normal 箭头头部形状。box、crow、diamond、dot、none、normal、vee。箭头文档点此
arrowtail   箭头尾部形状
arrowsize 1.0 箭头大小
style   图形样式,eg. bold、dashed、dotted、filled
lhead   当 compound 为true时,lhead用于指定边指向的cluster
ltail   与ltail相似

arrowhead示例

digraph demo {
    bgcolor="floralwhite"
    rankdir=LR

    "box"->"crow"[arrowhead=box]
    "crow"->"curve"[arrowhead=crow]
    "curve"->"diamond"[arrowhead=curve]
    "diamond"->"dot"[arrowhead=diamond]
    "dot"->"inv"[arrowhead=dot]
    "inv"->"none"[arrowhead=inv]
    "none"->"normal"[arrowhead=none]
    "normal"->"tee"[arrowhead=normal]
    "tee"->"vee"[arrowhead=tee]
    "vee"->"box"[arrowhead=vee]

    #来个高级的用法
    a->b[arrowhead=lcrowortee]
}

2.3 一些示例

2.3.1 子图

一个图能够包含多个子图,以及子图也能够嵌套子图。子图的名字须为cluster*,不然就直接当节点渲染了。

digraph demo {
    bgcolor="beige"

    subgraph cluster_husband {
        node[color="grey"]
        {"爸爸", "妈妈"} -> "我"
    }

     subgraph cluster_wife {
         {"岳父", "岳母"} -> "老婆"
     }

     "我" -> "老婆"[label="夫妻", dir="both"]
     {rank=same; "我", "老婆"}
}

渲染效果以下:

2.3.2 二叉树形式

digraph demo {
    bgcolor="beige"
    node [shape="record", height=.1]
    node0[label="<f0> | <f1> G | <f2>"]
    node1[label="<f0> | <f1> E | <f2>"]
    node2[label="<f0> | <f1> B | <f2>"]
    node0:f0 -> node1:f1
    node0:f2 -> node2:f1
}

其中,用 | 隔开的串会在绘制出来的节点中展示为一条分隔符,用 <> 括起来的串称为锚点。效果以下:

记录形式的节点也能够是竖形排列的。与横向排列的记录的不一样只是label的形式不一样,label中内容使用 {} 包围则是竖形排列的。代码以下:

digraph demo {
    bgcolor="beige"
    node [shape="record"]
    a [label="{a | b | c}"]
}

2.3.3 直接指向子图

边直接指向cluster,须要设置 compound 为true,并配合 lhead 或 ltail 来实现。代码以下:

digraph demo {
    bgcolor="beige"
    compound=true
    subgraph cluster0 {
        a
    }
    subgraph cluster1 {
        b
    }
    a -> b [lhead=cluster1];
}


Markdown可让使用者关注内容的书写,不太须要关注文档格式;Graphviz也是同样,让使用者更多的关注内容自己之间的关系而不用太在乎展示形式,以文本的方式来绘图也方便进行版本管理。当关系数据量比较大时(好比从数据库读取数据)Graphviz的优点将更明显。

参考文档:

相关文章
相关标签/搜索