Skip to content

Instantly share code, notes, and snippets.

@fredpyo
Created December 10, 2014 21:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fredpyo/ba4e6d56c5976302b83b to your computer and use it in GitHub Desktop.
Save fredpyo/ba4e6d56c5976302b83b to your computer and use it in GitHub Desktop.
pydot 001
# -*- coding: utf-8 -*-
"""
pydot example 1
@author: Federico Cáceres
@url: http://pythonhaven.wordpress.com/2009/12/09/generating_graphs_with_pydot
"""
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!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment