Graphviz 画图教程(Python)

系统 173 0

文章目录

      • 前言
      • Graphviz库
      • 开始
        • Digraph(一)
          • Dot代码
        • Digraph(二)
          • Dot代码
        • Digraph(三)
          • Dot代码
        • Source
          • Dot代码
      • 结语

前言

之前我的博客介绍了Graphviz 画图教程,虽然dot语法类似C语言容易编写和理解,但是这仅限于小图,当你想要画一个大图的时候,每一个结点都得一个个去定义名字、属性、连接线,这无疑是十分麻烦的,这种时候就想到了Python,能否利用Python语言编写一个画图脚本呢?

Graphviz库

幸运的是,Python正好有一个Graphviz库,Github地址
安装也是十分简单,只需要一行代码:
pip install graphviz

开始

Digraph(一)

本次博客的代码直接在Jupyter上运行,方便显示结果

            
              
                from
              
               graphviz 
              
                import
              
               Digraph

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

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

            
          

Graphviz 画图教程(Python)_第1张图片

Dot代码
            
              
                // The Round Table
              
              
digraph 
              
                {
              
              
	A 
              
                [
              
              label
              
                =
              
              
                "King Arthur"
              
              
                ]
              
              
	B 
              
                [
              
              label
              
                =
              
              
                "Sir Bedevere the Wise"
              
              
                ]
              
              
	L 
              
                [
              
              label
              
                =
              
              
                "Sir Lancelot the Brave"
              
              
                ]
              
              
	A 
              
                ->
              
               B
	A 
              
                ->
              
               L
	B 
              
                ->
              
               L 
              
                [
              
              constraint
              
                =
              
              false
              
                ]
              
              
                }
              
            
          

Digraph(二)

            
              
                from
              
               graphviz 
              
                import
              
               Digraph

f 
              
                =
              
               Digraph
              
                (
              
              
                'finite_state_machine'
              
              
                ,
              
               filename
              
                =
              
              
                'fsm.gv'
              
              
                )
              
              
f
              
                .
              
              attr
              
                (
              
              rankdir
              
                =
              
              
                'LR'
              
              
                ,
              
               size
              
                =
              
              
                '8,5'
              
              
                )
              
              

f
              
                .
              
              attr
              
                (
              
              
                'node'
              
              
                ,
              
               shape
              
                =
              
              
                'doublecircle'
              
              
                )
              
              
f
              
                .
              
              node
              
                (
              
              
                'LR_0'
              
              
                )
              
              
f
              
                .
              
              node
              
                (
              
              
                'LR_3'
              
              
                )
              
              
f
              
                .
              
              node
              
                (
              
              
                'LR_4'
              
              
                )
              
              
f
              
                .
              
              node
              
                (
              
              
                'LR_8'
              
              
                )
              
              

f
              
                .
              
              attr
              
                (
              
              
                'node'
              
              
                ,
              
               shape
              
                =
              
              
                'circle'
              
              
                )
              
              
f
              
                .
              
              edge
              
                (
              
              
                'LR_0'
              
              
                ,
              
              
                'LR_2'
              
              
                ,
              
               label
              
                =
              
              
                'SS(B)'
              
              
                )
              
              
f
              
                .
              
              edge
              
                (
              
              
                'LR_0'
              
              
                ,
              
              
                'LR_1'
              
              
                ,
              
               label
              
                =
              
              
                'SS(S)'
              
              
                )
              
              
f
              
                .
              
              edge
              
                (
              
              
                'LR_1'
              
              
                ,
              
              
                'LR_3'
              
              
                ,
              
               label
              
                =
              
              
                'S($end)'
              
              
                )
              
              
f
              
                .
              
              edge
              
                (
              
              
                'LR_2'
              
              
                ,
              
              
                'LR_6'
              
              
                ,
              
               label
              
                =
              
              
                'SS(b)'
              
              
                )
              
              
f
              
                .
              
              edge
              
                (
              
              
                'LR_2'
              
              
                ,
              
              
                'LR_5'
              
              
                ,
              
               label
              
                =
              
              
                'SS(a)'
              
              
                )
              
              
f
              
                .
              
              edge
              
                (
              
              
                'LR_2'
              
              
                ,
              
              
                'LR_4'
              
              
                ,
              
               label
              
                =
              
              
                'S(A)'
              
              
                )
              
              
f
              
                .
              
              edge
              
                (
              
              
                'LR_5'
              
              
                ,
              
              
                'LR_7'
              
              
                ,
              
               label
              
                =
              
              
                'S(b)'
              
              
                )
              
              
f
              
                .
              
              edge
              
                (
              
              
                'LR_5'
              
              
                ,
              
              
                'LR_5'
              
              
                ,
              
               label
              
                =
              
              
                'S(a)'
              
              
                )
              
              
f
              
                .
              
              edge
              
                (
              
              
                'LR_6'
              
              
                ,
              
              
                'LR_6'
              
              
                ,
              
               label
              
                =
              
              
                'S(b)'
              
              
                )
              
              
f
              
                .
              
              edge
              
                (
              
              
                'LR_6'
              
              
                ,
              
              
                'LR_5'
              
              
                ,
              
               label
              
                =
              
              
                'S(a)'
              
              
                )
              
              
f
              
                .
              
              edge
              
                (
              
              
                'LR_7'
              
              
                ,
              
              
                'LR_8'
              
              
                ,
              
               label
              
                =
              
              
                'S(b)'
              
              
                )
              
              
f
              
                .
              
              edge
              
                (
              
              
                'LR_7'
              
              
                ,
              
              
                'LR_5'
              
              
                ,
              
               label
              
                =
              
              
                'S(a)'
              
              
                )
              
              
f
              
                .
              
              edge
              
                (
              
              
                'LR_8'
              
              
                ,
              
              
                'LR_6'
              
              
                ,
              
               label
              
                =
              
              
                'S(b)'
              
              
                )
              
              
f
              
                .
              
              edge
              
                (
              
              
                'LR_8'
              
              
                ,
              
              
                'LR_5'
              
              
                ,
              
               label
              
                =
              
              
                'S(a)'
              
              
                )
              
              

f

            
          

Graphviz 画图教程(Python)_第2张图片

Dot代码
            
              digraph finite_state_machine 
              
                {
              
              
	rankdir
              
                =
              
              LR size
              
                =
              
              
                "8,5"
              
              
	node 
              
                [
              
              shape
              
                =
              
              doublecircle
              
                ]
              
              
	LR_0
	LR_3
	LR_4
	LR_8
	node 
              
                [
              
              shape
              
                =
              
              circle
              
                ]
              
              
	LR_0 
              
                ->
              
               LR_2 
              
                [
              
              label
              
                =
              
              
                "SS(B)"
              
              
                ]
              
              
	LR_0 
              
                ->
              
               LR_1 
              
                [
              
              label
              
                =
              
              
                "SS(S)"
              
              
                ]
              
              
	LR_1 
              
                ->
              
               LR_3 
              
                [
              
              label
              
                =
              
              
                "S($end)"
              
              
                ]
              
              
	LR_2 
              
                ->
              
               LR_6 
              
                [
              
              label
              
                =
              
              
                "SS(b)"
              
              
                ]
              
              
	LR_2 
              
                ->
              
               LR_5 
              
                [
              
              label
              
                =
              
              
                "SS(a)"
              
              
                ]
              
              
	LR_2 
              
                ->
              
               LR_4 
              
                [
              
              label
              
                =
              
              
                "S(A)"
              
              
                ]
              
              
	LR_5 
              
                ->
              
               LR_7 
              
                [
              
              label
              
                =
              
              
                "S(b)"
              
              
                ]
              
              
	LR_5 
              
                ->
              
               LR_5 
              
                [
              
              label
              
                =
              
              
                "S(a)"
              
              
                ]
              
              
	LR_6 
              
                ->
              
               LR_6 
              
                [
              
              label
              
                =
              
              
                "S(b)"
              
              
                ]
              
              
	LR_6 
              
                ->
              
               LR_5 
              
                [
              
              label
              
                =
              
              
                "S(a)"
              
              
                ]
              
              
	LR_7 
              
                ->
              
               LR_8 
              
                [
              
              label
              
                =
              
              
                "S(b)"
              
              
                ]
              
              
	LR_7 
              
                ->
              
               LR_5 
              
                [
              
              label
              
                =
              
              
                "S(a)"
              
              
                ]
              
              
	LR_8 
              
                ->
              
               LR_6 
              
                [
              
              label
              
                =
              
              
                "S(b)"
              
              
                ]
              
              
	LR_8 
              
                ->
              
               LR_5 
              
                [
              
              label
              
                =
              
              
                "S(a)"
              
              
                ]
              
              
                }
              
            
          

Digraph(三)

            
              
                from
              
               graphviz 
              
                import
              
               Digraph

g 
              
                =
              
               Digraph
              
                (
              
              
                'G'
              
              
                ,
              
               filename
              
                =
              
              
                'cluster_edge.gv'
              
              
                )
              
              
g
              
                .
              
              attr
              
                (
              
              compound
              
                =
              
              
                'true'
              
              
                )
              
              
                with
              
               g
              
                .
              
              subgraph
              
                (
              
              name
              
                =
              
              
                'cluster0'
              
              
                )
              
              
                as
              
               c
              
                :
              
              
    c
              
                .
              
              edges
              
                (
              
              
                [
              
              
                'ab'
              
              
                ,
              
              
                'ac'
              
              
                ,
              
              
                'bd'
              
              
                ,
              
              
                'cd'
              
              
                ]
              
              
                )
              
              
                with
              
               g
              
                .
              
              subgraph
              
                (
              
              name
              
                =
              
              
                'cluster1'
              
              
                )
              
              
                as
              
               c
              
                :
              
              
    c
              
                .
              
              edges
              
                (
              
              
                [
              
              
                'eg'
              
              
                ,
              
              
                'ef'
              
              
                ]
              
              
                )
              
              

g
              
                .
              
              edge
              
                (
              
              
                'b'
              
              
                ,
              
              
                'f'
              
              
                ,
              
               lhead
              
                =
              
              
                'cluster1'
              
              
                )
              
              
g
              
                .
              
              edge
              
                (
              
              
                'd'
              
              
                ,
              
              
                'e'
              
              
                )
              
              
g
              
                .
              
              edge
              
                (
              
              
                'c'
              
              
                ,
              
              
                'g'
              
              
                ,
              
               ltail
              
                =
              
              
                'cluster0'
              
              
                ,
              
               lhead
              
                =
              
              
                'cluster1'
              
              
                )
              
              
g
              
                .
              
              edge
              
                (
              
              
                'c'
              
              
                ,
              
              
                'e'
              
              
                ,
              
               ltail
              
                =
              
              
                'cluster0'
              
              
                )
              
              
g
              
                .
              
              edge
              
                (
              
              
                'd'
              
              
                ,
              
              
                'h'
              
              
                )
              
              

g

            
          

Graphviz 画图教程(Python)_第3张图片

Dot代码
            
              digraph G 
              
                {
              
              
	compound
              
                =
              
              true
	subgraph cluster0 
              
                {
              
              
		a 
              
                ->
              
               b
		a 
              
                ->
              
               c
		b 
              
                ->
              
               d
		c 
              
                ->
              
               d
	
              
                }
              
              
	subgraph cluster1 
              
                {
              
              
		e 
              
                ->
              
               g
		e 
              
                ->
              
               f
	
              
                }
              
              
	b 
              
                ->
              
               f 
              
                [
              
              lhead
              
                =
              
              cluster1
              
                ]
              
              
	d 
              
                ->
              
               e
	c 
              
                ->
              
               g 
              
                [
              
              lhead
              
                =
              
              cluster1 ltail
              
                =
              
              cluster0
              
                ]
              
              
	c 
              
                ->
              
               e 
              
                [
              
              ltail
              
                =
              
              cluster0
              
                ]
              
              
	d 
              
                ->
              
               h

              
                }
              
            
          

Source

            
              
                from
              
               graphviz 
              
                import
              
               Source

src 
              
                =
              
               Source
              
                (
              
              
                'digraph "the holy hand grenade" { rankdir=LR; 1 -> 2 -> 3 -> lob }'
              
              
                )
              
              

src


            
          

在这里插入图片描述

Dot代码
            
              digraph 
              
                "the holy hand grenade"
              
              
                {
              
               
	rankdir
              
                =
              
              LR
              
                ;
              
              
                1
              
              
                ->
              
              
                2
              
              
                ->
              
              
                3
              
              
                ->
              
               lob 

              
                }
              
            
          

结语

粗略地记录了下Graphviz库的一些语法及操作,更多内容可以看Graphviz文档


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请请扫描上面二维码支持博主1元、2元、5元等您想捐的金额吧,狠狠点击下面给点支持吧

发表我的评论
最新评论 总共0条评论