Generating Graph Visualizations with pydot and Graphviz

9 12 2009

Hi, for my latest college assignment I had to find a way to visualize data that is interrelated. For instance, my application generated the following data:

A --> B
B --> C
B --> D

And I needed a way to generate pretty graphs without too much headache!

I am already using wxPython for the application’s UI (it saved me a lot of time, and I learned a lot in the process, even implemented my own clone of the Aero Wizard layout used in Windows Vista and 7), so I tried to look for a way to integrate some other super powered library into my app. I quickly remembered a couple of projects that used Graphviz to generate visualizations of Django model definitions: DjangoGraphviz, and django-graphviz. I even hacked one of them to generate visualizations where each app had it’s own color. But these were complicated projects, because they generated some funky XML (correct me if I’m wrong) files that where later fed to graphviz and you had to mess a little with config options.

I wanted something simpler… lucky me, I stumbled into the wonderful project pydot. It was just what I needed, after a couple of experiments I could quickly generate “fantastic” visualizations with almost no extra effort. I’m using this on my Fedora 11 x86_64 box, so I just had to install pydot and easy_install handled the dependencies (it automatically downloaded and installed pyparsing), I am not sure what the requirements for Windows are… specially to get this running with py2exe, I’ll be blogging about that later.

In any case, let me provide you with a quick “getting started” tutorial guide on how this works.

Example 1: “Easy as Pie”

Ok, let’s try an easy one, suppose you have a set of data that you want to represent in a hierarchical way… Say: King, Lords and Vassals… Let’s try to graph that with as little code as we can:

Simple, huh? You should have a graph like this:

Graph generated by example 1

Graph generated by example 1

Now… that looks pretty boring, right? Let’s try something some more… colorful and with a different kind of graph.

Example 2: “Still Easy as Pie”

Ok, on the last example we made an undirected graph, let’s create a directed one now, and let’s also add some colors and labels on the edges between nodes. Say we have four nodes: A, B, C and D, were A points to B, B points to C, C points to D and D points back to A… let’s try that, ok?

That code generates this graph:

Graph generated in example 2

Graph generated in example 2

As you can see, the possibilities are endless, using the attributes you can set for each node, edge and other forms supported by the library, you can easily visualize your data that would otherwise be very hard to see.

One more thing, you can set the attributes when initializing the Node/Edge, or you can use the set_xyz method, for instance, instead of doing this:

pydot.Edge(node_d, node_a, label="and back we go again", labelfontcolor="#009933", fontsize="10.0", color="blue")

You could do this:

edge = pydot.Edge(node_d, node_a)
edge.set_label("and back we go again")

That can come in handy.

Well, I hope you found this useful as I have, and that this mini pydot tutorial helps you with this great library.

Don’t forget to visit these links:

Till next time!



33 responses

9 12 2009
Kenny Meyer

Nice demonstration! Creating such diagrams in a Django powered website is a nice idea…

9 12 2009

Indeed, it is both fun and informative! It’s always easier to see problems in your design when it is displayed in graphs. You should check out those two projects I mentioned at the beginning of the post. I’m not sure if the are the same… or if one is the fork of another…. or if they are completely different, but I remember I used django-graphviz and it was excellent, it worked really well!

3 06 2011

Good examples…
It will be perfect for my project.

12 07 2011

Thanks a lot!
It was not being easy for me to understand how to work with pydot, but after reading your post everything is clear🙂

12 07 2011

Glad to hear my humble two year old post is still useful🙂.

16 04 2012

Federico! Fancy seeing you here!

16 04 2012

Welcome to my humble (and not so updated) blog!

19 10 2011

Just what I needed, thanks🙂

25 01 2012

Federico … Is there a way to visualize the graph without saving it as a png file? In other words, can we use a GUI libraries like wx to read and display the graph from main memory? Thanks, Raj

16 04 2012

Hi Raj, I made an app with wxpython and pydot that “drew” graphs in realtime according to a grid that the user can use to model the graph ( Unfortunately the source code is in spanish, but let me tell you how I did it. I just created the graphs on a temporary file using python’s tempfile module and loaded the file using wxPython bitmap loading functions. Just remember to delete the file once you’re done.

23 02 2012
Alan Yeung

Cool, looking at directive graphs in Python right now, the python-graph looks good but couldnt get it working with the brief amount of time spent today. This pydot seems to work out of the box, sweet.

Did you ever get this runing with py2exe??

16 04 2012

Hi Alan, pydot is indeed very simple to get working and use. I don’t remember having to do anything special to make pydot work with py2exe, did you get it working?

28 02 2012

made so easy \m/
any pointers for pydot+boost to work in python?

16 04 2012

I’m glad this was useful for you! Unfortunately I have no experience on boost, sorry I can’t help you on that.

23 04 2012
Alan Yeung

Hi Federico,

I can build to .exe but it still requires the Graphviz.exe.

All the libs I have seen (pydot, pygraphviz) all need Graphviz installed on the computer. The issue I am having is directing Pydot or PyGraphviz to the relevant DLL’s which Py2exe pulls out correctly.

For example when I use PyGraphviz it references the Graphviz exe’s through an import _graphviz (which is .pyd file that calls the Graphviz .exe’s).

Still digging… in essence I would of thought that there was simple Pyhon libs to go from .dot to .svg without this Graphviz?? Also seen WinGraphviz.dll which I think is independant of Graphviz, just need to figure out how to use it.

26 09 2012
Mina Metias

Thank you so much, this was really straight to the point.
I wonder why pydot has such a poor documentation.

17 11 2012
Nate Rock

Thank you very much. This got me up and running with pydot in about 10 min. The one thing I didn’t do was have GraphViz installed before trying to run the code. After installing GraphViz it on Win 7 x64, it was smooth sailing.

23 07 2013

is there any way to use pyDot to create a tree and then build a GUI application in wx using the nodes in the tree as widgets?

5 08 2013
Dependencies | Eleven Eleven

[…] documentation on pydot on their google-code page was non-existent, so I visited this blog post which helped get me started. Then with some playing around in IDLE, I came up with […]

25 08 2013
Terrence Brannon

Excellent tutorial. Just what I needed. The dpaste links to source code no longer work. Perhaps make github gists of them?

16 12 2013
Embedded Components and Tools Blog Center » Blog Archive » Visualizing Software Tree Structures

[…] Some more graphviz links graphviz gallery (crazy examples here!): graphviz documentation: graphviz documentation on attributes: Generating Graph Visualizations with pydot and Graphviz […]

17 12 2013
Pydot and trees : easy way to draw trees in python

[…] The Pydot lib is uber easy to use and there is a quick example of  it s usage here. […]

3 12 2014

I am getting this error : File “”, line 33, in plot_graph
File “/usr/lib/python3/dist-packages/”, line 1809, in
lambda path, f=frmt, prog=self.prog : self.write(path, format=f, prog=prog))
File “/usr/lib/python3/dist-packages/”, line 1911, in write
dot_fd.write(self.create(prog, format))
File “/usr/lib/python3/dist-packages/”, line 2003, in create
stdout_output = ”.join(stdout_output)
TypeError: sequence item 0: expected str instance, bytes found

10 12 2014


These examples were built with Python 2 in mind, and you seem to be using Python 3, so I am afraid I cannot assist you debugging this issue. The error tells me this might be an issue with data types… I hope you got it fixed.

12 11 2015
Ricardo Solon Zalla

Federico, firstly thank you for the great source.
I’m developing a solution that build graphs as well and when tried to run your code I got the same error, in python 2:

Couldn’t import dot_parser, loading of dot files will not be possible.
Traceback (most recent call last):
File “/Users/zallaricardo/Documents/Python/”, line 32, in
File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/”, line 1602, in
lambda path, f=frmt, prog=self.prog : self.write(path, format=f, prog=prog))
File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/”, line 1696, in write
dot_fd.write(self.create(prog, format))
File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/”, line 1796, in create
status, stderr_output) )
pydot.InvocationException: Program terminated with status: -5. stderr follows: dyld: Library not loaded: /usr/lib/libltdl.7.dylib
Referenced from: /usr/local/bin/dot
Reason: image not found

Would you have any hint to fix it?

12 11 2015

Hi Ricardo, I’m glad this humble post is useful!

I’m not a MacOS user, but this issue seems to have been discussed over at Graphviz’s site:

Googling for
dyld: Library not loaded: /usr/lib/libltdl.7.dylib
Also reveals several solutions.

Hope it helps!

9 12 2014
A Dycks

This is the clearest explanation of use of the graphviz and pydot libraries that I have found to date. Kudos to the clarity and simplicity of your explanation. Big help with helping me diagram the system architecture of a web project for a recent Python Programmmig course that I’m enrolled. Well done!

11 03 2015

Another way to fix your first issue with “_noncomma” is to change pydot’s file so that the private _noncomma variable can be seen

Change this one line:

ParseException, ParseResults, CharsNotIn, _noncomma, dblQuotedString, QuotedString, ParserElement )

To these two lines:

ParseException, ParseResults, CharsNotIn, dblQuotedString, QuotedString, ParserElement )

_noncomma = “”.join( [ c for c in printables if c != “,” ] )

This answer comes from one the answers (which did not get voted as **the** answer) here:

13 03 2015

Hello fellow programmer,

I’m not sure what this comment refers to.

15 05 2015

Thanks for the post.

22 10 2015
Python:Generating hierarchy/tree diagrams in Python [closed] – IT Sprite

[…] For those who need some examples on how to use pydot, here is a link with examples that helped me a lot: […]

5 01 2016

Reblogged this on Tingting Zhao's research blog and commented:
This is a very useful post about how to use pydot to visualize graphical models. Pydot is a python interface for Graphviz.

8 02 2016
Buggy module installation: networkx & pygraphviz – Learning Python

[…] the net I found a chipper coder who offers his own tests for PyDot, Generating Graph Visualizations with pydot and Graphviz. In fact, I didn’t have PyDot and only added the package later. During installation of PyDot I […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: