﻿ Graphviz 画图教程（Python） - 军军小站|张军博客

# Graphviz 画图教程（Python）

### 文章目录

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

### Graphviz库

``` pip install graphviz ```

### 开始

#### Digraph（一）

```            ```

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

```
```

##### 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

```
```

##### 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'

,

=

'cluster1'

)

g

.

edge

(

'd'

,

'e'

)

g

.

edge

(

'c'

,

'g'

,

ltail

=

'cluster0'

,

=

'cluster1'

)

g

.

edge

(

'c'

,

'e'

,

ltail

=

'cluster0'

)

g

.

edge

(

'd'

,

'h'

)

g

```
```

##### Dot代码
```            ```
digraph G

{

compound

=

true
subgraph cluster0

{

a

->

b
a

->

c
b

->

d
c

->

d

}

subgraph cluster1

{

e

->

g
e

->

f

}

b

->

f

[

=

cluster1

]

d

->

e
c

->

g

[

=

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

{

rankdir

=

LR

;

1

->

2

->

3

->

lob

}

```
```

QQ号联系： 360901061

【本文对您有帮助就好】