Shankar
Shankar

Reputation: 3624

Duplicate edges created in Pygraphviz

I am using PyGraphviz to generate a hierarchical tree like structure with several levels and nodes. Whenever I try to create an edge between two nodes (I have unique index assigned to every node in the tree), Pygraphviz generates two edges, considering both the actual node value and the index whereas I am expecting from it, to only create an edge between node unique indices. Please look at the example code and figure below.

Sample code:

from pygraphviz import *

word_link = []
A = AGraph(directed=True)
ind = 0
A.add_node(ind, color='lightskyblue', style='filled', label='Root', shape='box')
sen_ind = ind + 1
# sentence 1
A.add_node(sen_ind, color='lightcoral', style='filled', label=0, shape='box')
A.add_edge(ind, sen_ind, color='plum', style='filled')
word_ind = sen_ind + 1
# word 1
A.add_node(word_ind, color='lightsalmon', style='filled', shape='box', label=0)
word_link.append(word_ind)
A.add_edge(sen_ind, word_ind, color='plum', style='filled')
word_ind += 1
# word 2
A.add_node(word_ind, color='lightsalmon', style='filled', shape='box', label=1)
A.add_edge(sen_ind, word_ind, color='plum', style='filled')
sen_ind = word_ind + 1
# sentence 2
A.add_node(sen_ind, color='lightcoral', style='filled', label=1, shape='box')
A.add_edge(ind, sen_ind, color='plum', style='filled')
word_ind = sen_ind + 1
# word 1
A.add_node(word_ind, color='lightsalmon', style='filled', label=0, shape='box')
A.add_edge(sen_ind, word_ind, color='plum', style='filled')
word_ind += 1
# word 2
A.add_node(word_ind, color='lightsalmon', style='filled', label=1, shape='box')
word_link.append(word_ind)
A.add_edge(sen_ind, word_ind, color='plum', style='filled')

# doesn't work | need a fix
A.add_edge(word_link[0], word_link[1], color='sienna', style='filled')

A.layout()  # layout with default (neato)
A.draw('simple.png',prog='dot') # draw png

Tree generated with duplicate edges enter image description here

Expected Figure: enter image description here

Upvotes: 0

Views: 1455

Answers (2)

Dr Sokoban
Dr Sokoban

Reputation: 1638

You can also try to define ranks (levels)

see this answer another use gave me for a similar question:

Pygraphviz / networkx set node level or layer

Upvotes: 0

marapet
marapet

Reputation: 56466

Try adding constraint=False:

A.add_edge(word_link[0], word_link[1], constraint=False, color='sienna', style='filled')

Upvotes: 1

Related Questions