6

I am trying to add parallel edges between two nodes using NetworkX but it fails with the below error. What am I doing wrong?

import networkx as nx
import graphviz

g1 = nx.MultiGraph()

node1 = 'a'
node2 = 'b'

g1.add_edge(node1,node2,key='one')
g1.add_edge(node1,node2,key='two')

A = nx.to_agraph(g1)
A.add_subgraph()

A.draw('test2.png', prog='dot')

Error:

Traceback (most recent call last):
  File "test2.py", line 12, in <module>
    A = nx.to_agraph(g1)
  File "C:\python27\lib\site-packages\networkx-1.11rc1-py2.7.egg\networkx\drawing\nx_agraph.py", line 152, in to_agraph
    A.add_edge(u,v,key=str(key),**str_edgedata)
  File "C:\python27\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'
TylerH
  • 20,799
  • 66
  • 75
  • 101
TraderMoe
  • 93
  • 1
  • 8
  • Super old question, but I think networkx DiGraphs don't allow parallel edges. So when you add the edge with key='two', it doesn't actually get added to the graph. – Jon Apr 05 '21 at 19:35

2 Answers2

4

You can do the same without using graphviz. I do it adding connectionstyle to nx.draw:

import networkx as nx

g1 = nx.DiGraph(directed=True)

node1 = 'a'
node2 = 'b'

g1.add_edge(node1,node2,key=1)
g1.add_edge(node2,node1,key=2)

nx.draw(g1, with_labels=True, arrows = True, connectionstyle='arc3, rad = 0.1')

See here the result

AMangipinto
  • 521
  • 5
  • 7
1

Your code is working fine, and I attached the output image.

enter image description here

Community
  • 1
  • 1
Abdallah Sobehy
  • 2,881
  • 1
  • 15
  • 28
  • Thanks for your response Abdallah. That doesn't work. It still gives me the same error. Have you actually tried that code? And does it work for you? – TraderMoe Jan 26 '16 at 04:55
  • Sorry my bad I made mistake – Abdallah Sobehy Jan 26 '16 at 04:58
  • Yes, I did not copy/paste my node definitions the first time. But it still doesn't work. I know my syntax is correct because if I make both keys to be the same value, it doesn't throw an error regarding syntax. Maybe I'm missing something in the installation of the packages. – TraderMoe Jan 26 '16 at 05:10
  • Actually your code worked, and I have attached the image maybe try to reinstall the packages – Abdallah Sobehy Jan 26 '16 at 06:34
  • I also tried the code and it works, however the import should be `pygraphviz` in my case. – Kirell Jan 26 '16 at 09:52
  • Thank you both for your verifying the code. Importing pygraphviz instead of graphviz or importing both still gives me the same error. There's nothing I can find on this error message. Pygraphviz works fine for other scripts that I wrote. I'm doing this on Windows. Will try a linux machine and see how it goes. – TraderMoe Jan 26 '16 at 16:54
  • Similar issue than: http://stackoverflow.com/questions/35610736/passing-a-string-to-agedge-in-agraph-py-issue-with-networkx-and-pygraphviz, it is maybe compilation error or Microsoft Windows relative bug - which windows version do you use ? Regards – A. STEFANI Aug 04 '16 at 16:59