1

I'm drawing graphs in python using graphviz. The pictures I get are almost as I want them, with one exception: the position of (some of) the nodes are not as I'd like them to be. Here is my example:

import graphviz as gv

A=[[1],[2,3,5,7],[4,6,9,10],[8]]
G=gv.Digraph(format='png',filename='Test')
for k in range(len(A)-1):
    for l in A[k]:
        G.node(str(l))
        for m in A[k+1]:
            if m%l==0:
                G.edge(str(l),str(m))
G.view()

And that's the result:

enter image description here

My problem here is that I want the nodes of the same rank to be ordered by magnitude, so that "2" is the leftmost node of rank 1 (starting from rank 0), "4" is the leftmost node of rank 2, etc.

Thanks for answers!

Martin

Martin Monath
  • 117
  • 1
  • 1
  • 7
  • I should add that I know that there are already a few similar questions already asked in stackoverflow around this topic. However, to my opinion, none of these question really fits my needs. I would already be glad if somebody finds an already answered question that would somehow help me...^^ – Martin Monath Apr 22 '17 at 21:13
  • Uhhhm... Hello? Anbody out there?^^ – Martin Monath Apr 27 '17 at 16:58

1 Answers1

2

Well, seems that I found an answer by myself. Indeed, I used the method of this already existing post together with the fact that one can directly create a .dot-file with python which was inspired by the Wikipedia-page on the Collatz-conjecture. Last but not least, by chance, I found that when installing graphviz, I also installed gvedit with which I could choose the layout engine and as the above post suggests, it has to be either fdp or neato (both of them worked for me). Here is my solution:

A=[[1],[2,3,5,7],[4,6,9,10],[8]]

dotfile = file('image.dot', 'w')
dotfile.write('Digraph{\n')
for k in range(len(A)):
    for l in range(len(A[k])):
        dotfile.write(str(A[k][l])+'[pos="'+str(l)+',-'+str(k)+'!"];\n')
for k in range(len(A)-1):
    for l in A[k]:
        for m in A[k+1]:
            if m%l==0:
                dotfile.write(str(l)+'->'+str(m)+';\n')
dotfile.write('}\n')
dotfile.close()

ordered

Community
  • 1
  • 1
Martin Monath
  • 117
  • 1
  • 1
  • 7