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:
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
?