Python 3 networkx draw_graphviz () not working

I just want to draw a simple graph with Python 3 networkx and graphviz:

import networkx as nx G = nx.complete_graph(3) nx.draw_graphviz(G) 

I use ubuntu14.04 and IPython3, and as usual, I did pip3 by installing networkx and executing the code:

 ImportError: pydot could not be loaded: http://code.google.com/p/pydot/ 

And I tried installing pydotplus and running the code:

 /usr/local/lib/python3.4/dist-packages/networkx/drawing/nx_pydot.py in pydot_layout(G, prog, root, **kwds) 294 295 if isinstance(node,list): --> 296 node=node[0] 297 pos=node.get_pos()[1:-1] # strip leading and trailing double quotes 298 if pos != None: IndexError: list index out of range 

and pydot2 also:

 /usr/local/lib/python3.4/dist-packages/pydot.py in write(self, path, prog, format) 1893 prog = self.prog 1894 -> 1895 dot_fd = file(path, "w+b") 1896 if format == 'raw': 1897 data = self.to_string() NameError: name 'file' is not defined 

I spent quite a bit of time searching and installing other pydots and pygraphviz combinations, but still no luck.

Although this may be due to: pydot and graphviz error: failed to import dot_parser, loading point files will be impossible , which does not solve the problem in Python 3.

+5
source share
3 answers

You can fix this by editing line #292 with

  pydot_node = pydot.Node(make_str(n)).get_name().encode('utf-8') 

to remove the encoding at the end:

  pydot_node = pydot.Node(make_str(n)).get_name() #.encode('utf-8') 

I reported this bug / fix here .

+2
source

This seems to be the same issue as the pydot you are using is a version not compatible with Python 3 that uses the file (...). file (...) has already been removed in Python 3.

I noticed this problem and installed a Python 3 compatible version on PyPi.

For Linux systems for Python 3.x, try:

pip3 install pydot3

Or generally for Python 2.x try:

pip install pydot3

+2
source

Not a very big answer, but it acts like a workaround.

First output the .dot file networkx.write_dot(G, 'graph.dot') using Python

and then run the appropriate graphviz command command, for example neato -T png graph.dot > graph.png on the command line.

+1
source

All Articles