-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpydottest.py
35 lines (29 loc) · 1.43 KB
/
pydottest.py
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
import pydot # import pydot or you're not going to get anywhere my friend :D
# first you create a new graph, you do that with pydot.Dot()
graph = pydot.Dot(graph_type='graph')
# the idea here is not to cover how to represent the hierarchical data
# but rather how to graph it, so I'm not going to work on some fancy
# recursive function to traverse a multidimensional array...
# I'm going to hardcode stuff... sorry if that offends you
# let's add the relationship between the king and vassals
for i in range(3):
# we can get right into action by "drawing" edges between the nodes in our graph
# we do not need to CREATE nodes, but if you want to give them some custom style
# then I would recomend you to do so... let's cover that later
# the pydot.Edge() constructor receives two parameters, a source node and a destination
# node, they are just strings like you can see
edge = pydot.Edge("king", "lord%d" % i)
# and we obviosuly need to add the edge to our graph
graph.add_edge(edge)
# now let us add some vassals
vassal_num = 0
for i in range(3):
# we create new edges, now between our previous lords and the new vassals
# let us create two vassals for each lord
for j in range(2):
edge = pydot.Edge("lord%d" % i, "vassal%d" % vassal_num)
graph.add_edge(edge)
vassal_num += 1
# ok, we are set, let's save our graph into a file
graph.write_png('example1_graph.png')
# and we are done!