原文: Drawing Graphs using Dot and Graphviz

1 License

Copyright (C) 2013, 2014, 2015, 2016, 2017, 2018 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 Introduction

2.1 What is DOT?

DOT is a plain text graph description language. It is a simple way of describing graphs that both humans and computer programs can read.

2.2 What is Graphviz?

Graphviz is open source graph visualization software. Graph visualization is a way of representing structural information as diagrams of abstract graphs and networks.

2.3 Who is this document for?

This document was originally written as quick reference for myself. It was then extended to become a tutorial for Computing students. It's now offered to anyone who wants to learn DOT by example.

2.4 Related Materials

You can find links to similar documents posts at my blog tech.tonyballantyne.com

If you have stumbled across this document by accident whilst looking for my work as an SF and Fantasy writer, the following links may be more useful to you

3 Setup

You will need to have the Graphviz suite of programs installed on your computer to follow this tutorial. Graphiz can be downloaded for free from the Graphviz site: http://www.graphviz.org/Home.php

4 Basic Examples

4.1 Simple Graph

  1. graph graphname {
  2. a -- b;
  3. b -- c;
  4. b -- d;
  5. d -- a;
  6. }

4.2 Same Graph, Different Layout

  1. graph graphname {
  2. rankdir=LR; //Rank Direction Left to Right
  3. a -- b;
  4. b -- c;
  5. b -- d;
  6. d -- a;
  7. }

4.3 Simple Digraph (Directional Graph)

  1. digraph graphname{
  2. a -> b;
  3. b -> c;
  4. a -> c;
  5. }

4.4 Simple Digraph with Labels

  1. digraph graphname{
  2. T [label="Teacher"] // node T
  3. P [label="Pupil"] // node P
  4. T->P [label="Instructions", fontcolor=darkgreen] // edge T->P
  5. }

4.5 Same Graph, Different Shape and Colour

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

Here are some of the shapes you can use… box, polygon, ellipse, oval, circle, point, egg, triangle, plaintext, diamond, trapezium, parallelogram, house, pentagon, hexagon, septagon, octagon, doublecircle, doubleoctagon, tripleoctagon

There are lots more available here… http://www.graphviz.org/content/node-shapes

4.6 Summary

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

5 More Advanced

5.1 Saving Time

It takes time defining each node individually. The following way is quicker

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

5.2 Records

You can now use HTML to define these sort of blocks. Find out more at http://www.graphviz.org/doc/info/shapes.html

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

6 Some Example Graphs

6.1 Finite State Machine

  1. digraph finite_state_machine {
  2. rankdir=LR;
  3. size="8,5"
  4. node [shape = circle];
  5. S0 -> S1 [ label = "Lift Nozzle" ]
  6. S1 -> S0 [ label = "Replace Nozzle" ]
  7. S1 -> S2 [ label = "Authorize Pump" ]
  8. S2 -> S0 [ label = "Replace Nozzle" ]
  9. S2 -> S3 [ label = "Pull Trigger" ]
  10. S3 -> S2 [ label = "Release Trigger" ]
  11. }

6.2 Data Flow Diagrams

  1. digraph dfd{
  2. node[shape=record]
  3. store1 [label="<f0> left|<f1> Some data store"];
  4. proc1 [label="{<f0> 1.0|<f1> Some process here\n\n\n}" shape=Mrecord];
  5. enti1 [label="Customer" shape=box];
  6. store1:f1 -> proc1:f0;
  7. enti1-> proc1:f0;
  8. }

6.3 Data Flow Diagrams 2

The following uses subgraphs to display different levels. Note that subgraphs must start with the prefix cluster_ or they won't work. It will only work with dot layout.

  1. digraph dfd2{
  2. node[shape=record]
  3. subgraph level0{
  4. enti1 [label="Customer" shape=box];
  5. enti2 [label="Manager" shape=box];
  6. }
  7. subgraph cluster_level1{
  8. label ="Level 1";
  9. proc1 [label="{<f0> 1.0|<f1> One process here\n\n\n}" shape=Mrecord];
  10. proc2 [label="{<f0> 2.0|<f1> Other process here\n\n\n}" shape=Mrecord];
  11. store1 [label="<f0> |<f1> Data store one"];
  12. store2 [label="<f0> |<f1> Data store two"];
  13. {rank=same; store1, store2}
  14. }
  15. enti1 -> proc1
  16. enti2 -> proc2
  17. store1 -> proc1
  18. store2 -> proc2
  19. proc1 -> store2
  20. store2 -> proc1
  21. }

6.4 Object Inheritance

  1. digraph obj{
  2. node[shape=record];
  3. rankdir="BT";
  4. teacher [label = "{<f0> Teacher|<f1> \n |<f2> \n }"];
  5. course [label = "{<f0> Course|<f1> \n |<f2> \n }"];
  6. student [label = "{<f0> Student|<f1> \n |<f2> \n }"];
  7. lesson [label = "{<f0> Lesson |<f1> \n |<f2> \n }"];
  8. tutorial [label = "{<f0> Tutorial|<f1> \n |<f2> \n }"];
  9. assessment[label = "{<f0> Assessment|<f1> \n |<f2> \n }"];
  10. coursework [label = "{<f0> Coursework|<f1> \n |<f2> \n }"];
  11. exam [label = "{<f0> Exam|<f1> \n |<f2> \n }"];
  12. {rank=same; teacher course student}
  13. teacher->course [dir="forward",arrowhead="none",arrowtail="normal",headlabel="1",taillabel="1.."];
  14. student->course [dir="forward",arrowhead="none",arrowtail="normal",headlabel="1",taillabel="1.."];
  15. lesson->course [dir="forward",arrowhead="diamond",arrowtail="normal"];
  16. tutorial->course [dir="forward",arrowhead="diamond",arrowtail="normal"];
  17. assessment->course [dir="forward",arrowhead="diamond",arrowtail="normal"];
  18. coursework->assessment;
  19. exam->assessment;
  20. }

6.5 Entity Relationship

  1. digraph ER{
  2. node[shape=box];
  3. Book;
  4. Customer;
  5. Loan;
  6. {rank=same;Book,Customer,Loan}
  7. Book->Loan[dir="forward",arrowhead="crow",arrowtail="normal"];
  8. Customer->Loan[dir="forward",arrowhead="crow",arrowtail="normal"];
  9. }

7 Reference

Here are the most useful attributes you will need when drawing graphs. The full list can be found here: http://graphviz.org/doc/info/attrs.html

7.1 Graph Attributes

  • label="My Graph"; Label a graph itself
  • rankdir=LR; Lay the graph out from Left to Right, instead of Top to Bottom
  • {rank=same; a, b, c } Group nodes together at the same level of a graph
  • splines="line"; Force edges to be straight, no curves or angles
  • K=0.6; Used to influence the 'spring' used in the layout, Can be used to push nodes further apart, which is especially useful for twopi and sfdp layouts

7.2 Vertex Attributes

  • [label="Some Label"] Labels the Vertex
  • [color="red"] Colors the Vertex
  • [fillcolor="blue"] Fills the Vertex with the specified colour

7.3 Edge Attributes

  • [label="Some Label"] Labels the Edge (Useful for Weights)
  • [color="red"] Colors the Vertex (Useful for Paths)
  • [penwidth=2.0] Adjusts the thickness of the edge line, Very useful for Paths

7.4 Size, Background Colour

fixedsize=true; size="1,1"; resolution=72; bgcolor="#C6CFD532";

8 Appendices

8.1 Further Reading

8.2 Using Emacs Org Mode

Emacs org mode is an ideal environment for writing, executing and exporting Dot graphics

8.2.1 Setup

Download and install graphviz and add the path to the exec-path variable

You will need to update your .emacs file to load dot as a babel language. The following is a useful babel setup for dot and other languages

  1. (org-babel-do-load-languages
  2. (quote org-babel-load-languages)
  3. (quote ((emacs-lisp . t)
  4. (java . t)
  5. (dot . t)
  6. (ditaa . t)
  7. (R . t)
  8. (python . t)
  9. (ruby . t)
  10. (gnuplot . t)
  11. (clojure . t)
  12. (sh . t)
  13. (ledger . t)
  14. (org . t)
  15. (plantuml . t)
  16. (latex . t))))

8.2.2 Embedding Dot in Emacs

Org mode can interpret different languages by using the Library Of Babel. To do so, enclose the code in begin_ src and end_ src tags as below. You'll need to add command line arguments as shown.

A shortcut to make a begin_ src block is to type <s [TAB]

  1. #+begin_src dot :file ./img/example1.png :cmdline -Kdot -Tpng
  2. graph graphname {
  3. a -- b;
  4. b -- c;
  5. b -- d;
  6. d -- a;
  7. }
  8. #+end_src

8.2.3 The Command Line

The section :cmdline -Kdot -Tpng in the #+begin_ src dot :file ./img/example1.png :cmdline -Kdot -Tpng section are command line arguments. They tell dot how to render and display.

  • -Kdot use dot layout. The other layouts you can try are Kneato, Kcirco, Ktwopi, Kfdp and Ksfdp for different layouts
  • -Tpng render as png

The full command line arguments can be found here: http://graphviz.org/content/command-line-invocation

Date: <2013-10-21 Mon>

Author: Tony Ballantyne

Created: 2018-09-01 Sat 12:33

Emacs 23.3.1 (Org mode 8.0.2)

Validate

MARSGGBO♥原创







2019-1-27

【转载】使用python库--Graphviz为论文画出漂亮的示意图的更多相关文章

  1. 用JAVA中BufferedImage画出漂亮的验证码点击变化

    如果我们想用JAVA中BufferedImage画出漂亮的验证码点击变化怎么实现呢,类似这样: 点击变化,以下是实现过程,直接上代码: 首先前台:<i><img style=&quo ...

  2. (转载) Chrome中canvas上drawImage无法画出image的解决办法

    在自己写demo的过程中 碰到了这样一个问题 发现drawImage方法没有达到预期的效果 图片没办法显示 而fillRect等画图形的方法却工作良好 大概的代码如下: $(function() { ...

  3. 用Python画如此漂亮的专业插图 ?简直So easy!

    本文整理自知乎问答,仅用于学术分享,著作权归作者所有.如有侵权,请联系我删文处理.多多转发,多多学习! 方法一 强烈推荐 Python 的绘图模块 matplotlib: python plottin ...

  4. [转载]花了半个月,终于把Python库全部整理出来了,非常全面

    库名称简介 Chardet 字符编码探测器,可以自动检测文本.网页.xml的编码. colorama 主要用来给文本添加各种颜色,并且非常简单易用. Prettytable 主要用于在终端或浏览器端构 ...

  5. Python 库打包分发、setup.py 编写、混合 C 扩展打包的简易指南(转载)

    转载自:http://blog.konghy.cn/2018/04/29/setup-dot-py/ Python 有非常丰富的第三方库可以使用,很多开发者会向 pypi 上提交自己的 Python ...

  6. 【转载】Python第三方库资源

    转自:https://weibo.com/ttarticle/p/show?id=2309404129469920071093 参考:https://github.com/jobbole/awesom ...

  7. 课程作业——Python基础之使用turtle库画出红旗

    代码如下: import turtle # 设置画笔和背景颜色 turtle.color('yellow') turtle.bgcolor('red') # 通过偏移量和尺寸大小画星星 def dra ...

  8. Python 库大全

    作者:Lingfeng Ai链接:http://www.zhihu.com/question/24590883/answer/92420471来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非 ...

  9. Python库资源大全

    转载地址:https://zhuanlan.zhihu.com/p/27350980 本文是一个精心设计的Python框架.库.软件和资源列表,是一个Awesome XXX系列的资源整理,由BigQu ...

随机推荐

  1. mac host文件配置

    Shift+Command+G 三个组合按键,并输入 Hosts 文件的所在路径:/etc/hosts /private/etc/hosts

  2. centos 7 安装appache 服务器

    一.安装Apache程序,一般有三种安装方式:1.直接网络安装:2.下载rpm包,上传至服务器进行安装:3.通过原代码编译安装: yum -y install httpd rpm -qa | grep ...

  3. (二叉树 bfs) leetcode 199. Binary Tree Right Side View

    Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...

  4. 谈.Net委托与线程——创建无阻塞的异步调用(一)

    前言 本文大部分内容来自于mikeperetz的Asynchronous Method Invocation及本人的一些个人体会所得,希望对你有所帮助.原英文文献可以在codeproject中搜索到. ...

  5. 剑指Offer_编程题_1

    题目描述 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数.   class Sol ...

  6. linux报错汇总

    一.出现cannot send message: Process exited with a non-zero status错误 查看log文件:sudo cat /var/log/mail.err, ...

  7. MySQL_函数(待续)

    1.REPLACE(str,from_str,to_str) 定义:REPLACE(str,from_str,to_str) 解释:返回值是把字符串str 中的子串from_str 全部替换为to_s ...

  8. linux下查看主板内存槽与内存信息

    1.查看内存槽数.那个槽位插了内存,大小是多少 dmidecode|grep -P -A5 "Memory\s+Device"|grep Size|grep -v Range 2. ...

  9. PHP索引数组+unset使用不当导致的问题

    转自先知社区 https://xz.aliyun.com/t/2443 0x00前言 通常网站后台可以配置允许上传附件的文件类型,一般登录后台,添加php类型即可上传php文件getshell.但是, ...

  10. Python获取下载速度并显示进度条

    #!/usr/bin/python3 # -*- coding:utf-8 -*- import sys import time from urllib import request ''' urll ...