sudo apt-get install graphviz
Examples from: https://github.com/xflr6/graphviz/tree/master/examples
In [2]:
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
Out[2]:
In [3]:
from graphviz import Source
src = Source('digraph "the holy hand grenade" { rankdir=LR; 1 -> 2 -> 3 -> lob }')
src
Out[3]:
In [4]:
# http://www.graphviz.org/content/fsm
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
Out[4]:
In [5]:
# http://www.graphviz.org/pdf/dotguide.pdf Figure 20
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
Out[5]:
In [8]:
from graphviz import Digraph
g = Digraph('G', filename='hello.gv')
g.edge('Hello', 'World')
g
Out[8]:
In [9]:
# angles.py - http://www.graphviz.org/Gallery/gradient/angles.html
from graphviz import Digraph
g = Digraph('G', filename='angles.gv')
g.attr(bgcolor='blue')
with g.subgraph(name='cluster_1') as c:
c.attr(fontcolor='white')
c.attr('node', shape='circle', style='filled', fillcolor='white:black',
gradientangle='360', label='n9:360', fontcolor='black')
c.node('n9')
for i, a in zip(range(8, 0, -1), range(360 - 45, -1, -45)):
c.attr('node', gradientangle='%d' % a, label='n%d:%d' % (i, a))
c.node('n%d' % i)
c.attr(label='Linear Angle Variations (white to black gradient)')
with g.subgraph(name='cluster_2') as c:
c.attr(fontcolor='white')
c.attr('node', shape='circle', style='radial', fillcolor='white:black',
gradientangle='360', label='n18:360', fontcolor='black')
c.node('n18')
for i, a in zip(range(17, 9, -1), range(360 - 45, -1, -45)):
c.attr('node', gradientangle='%d' % a, label='n%d:%d' % (i, a))
c.node('n%d' % i)
c.attr(label='Radial Angle Variations (white to black gradient)')
g.edge('n5', 'n14')
g
Out[9]:
In [10]:
# unix.py - http://www.graphviz.org/content/unix
from graphviz import Digraph
u = Digraph('unix', filename='unix.gv')
u.attr(size='6,6')
u.node_attr.update(color='lightblue2', style='filled')
u.edge('5th Edition', '6th Edition')
u.edge('5th Edition', 'PWB 1.0')
u.edge('6th Edition', 'LSX')
u.edge('6th Edition', '1 BSD')
u.edge('6th Edition', 'Mini Unix')
u.edge('6th Edition', 'Wollongong')
u.edge('6th Edition', 'Interdata')
u.edge('Interdata', 'Unix/TS 3.0')
u.edge('Interdata', 'PWB 2.0')
u.edge('Interdata', '7th Edition')
u.edge('7th Edition', '8th Edition')
u.edge('7th Edition', '32V')
u.edge('7th Edition', 'V7M')
u.edge('7th Edition', 'Ultrix-11')
u.edge('7th Edition', 'Xenix')
u.edge('7th Edition', 'UniPlus+')
u.edge('V7M', 'Ultrix-11')
u.edge('8th Edition', '9th Edition')
u.edge('1 BSD', '2 BSD')
u.edge('2 BSD', '2.8 BSD')
u.edge('2.8 BSD', 'Ultrix-11')
u.edge('2.8 BSD', '2.9 BSD')
u.edge('32V', '3 BSD')
u.edge('3 BSD', '4 BSD')
u.edge('4 BSD', '4.1 BSD')
u.edge('4.1 BSD', '4.2 BSD')
u.edge('4.1 BSD', '2.8 BSD')
u.edge('4.1 BSD', '8th Edition')
u.edge('4.2 BSD', '4.3 BSD')
u.edge('4.2 BSD', 'Ultrix-32')
u.edge('PWB 1.0', 'PWB 1.2')
u.edge('PWB 1.0', 'USG 1.0')
u.edge('PWB 1.2', 'PWB 2.0')
u.edge('USG 1.0', 'CB Unix 1')
u.edge('USG 1.0', 'USG 2.0')
u.edge('CB Unix 1', 'CB Unix 2')
u.edge('CB Unix 2', 'CB Unix 3')
u.edge('CB Unix 3', 'Unix/TS++')
u.edge('CB Unix 3', 'PDP-11 Sys V')
u.edge('USG 2.0', 'USG 3.0')
u.edge('USG 3.0', 'Unix/TS 3.0')
u.edge('PWB 2.0', 'Unix/TS 3.0')
u.edge('Unix/TS 1.0', 'Unix/TS 3.0')
u.edge('Unix/TS 3.0', 'TS 4.0')
u.edge('Unix/TS++', 'TS 4.0')
u.edge('CB Unix 3', 'TS 4.0')
u.edge('TS 4.0', 'System V.0')
u.edge('System V.0', 'System V.2')
u.edge('System V.2', 'System V.3')
u
Out[10]:
In [12]:
from graphviz import Digraph
g = Digraph('G', filename='cluster.gv')
# NOTE: the subgraph name needs to begin with 'cluster' (all lowercase)
# so that Graphviz recognizes it as a special cluster subgraph
with g.subgraph(name='cluster_0') as c:
c.attr(style='filled')
c.attr(color='lightgrey')
c.node_attr.update(style='filled', color='white')
c.edges([('a0', 'a1'), ('a1', 'a2'), ('a2', 'a3')])
c.attr(label='process #1')
with g.subgraph(name='cluster_1') as c:
c.node_attr.update(style='filled')
c.edges([('b0', 'b1'), ('b1', 'b2'), ('b2', 'b3')])
c.attr(label='process #2')
c.attr(color='blue')
g.edge('start', 'a0')
g.edge('start', 'b0')
g.edge('a1', 'b3')
g.edge('b2', 'a3')
g.edge('a3', 'a0')
g.edge('a3', 'end')
g.edge('b3', 'end')
g.node('start', shape='Mdiamond')
g.node('end', shape='Msquare')
g
Out[12]:
In [ ]: