2

I am using python3.4, networkx2.2, graphviz2.38 and pygraphviz1.3 for creating and showing graphs in python. I have the following piece of code that correctly writes the multigraph to a png file.

import networkx as nx
import pylab as plt
from networkx.drawing.nx_agraph import graphviz_layout, to_agraph
from networkx.drawing.nx_pydot import write_dot, to_pydot

ic_G = nx.MultiDiGraph()
ic_G.add_nodes_from(['A', 'B', 'C'])
nx.add_path(ic_G, ['A', 'B', 'C'])
#ic_G.add_edge('A','B')

dot_G = to_pydot(ic_G)
print(dot_G)


A = to_agraph(ic_G)
A.layout('dot')
A.draw('abc.png')

The console shows the multigraph in correct format as shown below

digraph  {
B;
A;
C;
B -> C  [key=0];
A -> B  [key=0];
}

and the following file is written in the same directory:

multidigraph

The problem arises when I add duplicate edges in the multigraph i.e when I un-comment the following line from the code above

#ic_G.add_edge('A','B')

I get the following exception on the console

Traceback (most recent call last): File "C:\Users\Basit\Anaconda3\envs\python34\lib\site-packages\pygraphviz\agraph.py", line 478, in add_edge eh = gv.agedge(self.handle, uh, vh, key, _Action.create) KeyError: 'agedge: no key'

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "D:/GitHub/misc/BP_Graphs/test2.py", line 16, in A = to_agraph(ic_G) File "C:\Users\Basit\Anaconda3\envs\python34\lib\site-packages\networkx\drawing\nx_agraph.py", line 161, in to_agraph A.add_edge(u, v, key=str(key)) File "C:\Users\Basit\Anaconda3\envs\python34\lib\site-packages\pygraphviz\agraph.py", line 481, in add_edge eh = gv.agedge(self.handle, uh, vh, key, _Action.find) KeyError: 'agedge: no key'

The to_pydot function works fine and console prints the multiple edges of multigraph in dot format as shown below

digraph  {
B;
C;
A;
B -> C  [key=0];
A -> B  [key=0];
A -> B  [key=1];
}

My question is, why to_agraph is failing for multiple edges in a multigraph? Is there a way to convert the output of to_pydot function into a png file without having to use to_agraph?

zohar.kom
  • 1,765
  • 3
  • 12
  • 28
Muhammad Adeel Zahid
  • 17,474
  • 14
  • 90
  • 155
  • `Dot` object can be written to `png` file using `write_png` function. So only question that remains is why `to_agraph` is throwing exception in case of multiple edges? – Muhammad Adeel Zahid Sep 26 '18 at 10:14
  • I can't reproduce your error with either python 2.7 or python 3.5; both produce the multi-graph version ok. This github issue looks rather similar: https://github.com/networkx/networkx/issues/2012 and a main developer indicates the problem is within (py)graphviz. – Bonlenfum Sep 28 '18 at 09:29
  • what version of `networkx` and `pygraphviz` you are using? I also suspect it to be `pygraphviz` or `graphviz` issue. I was able to render the `Multigraph` with `pydot` just fine. – Muhammad Adeel Zahid Oct 01 '18 at 09:41
  • in py2, pygraphviz is 1.3.1, networkx is 2.1. in py3, pygraphviz is 1.5, networkx=2.2. – Bonlenfum Oct 03 '18 at 12:13

0 Answers0