5

I am trying to draw a 100 node multi-graph G in graphviz layout in python's networkx so I made two trials so far:

Trial 1 nx.draw_graphviz function as follows nx.draw_graphviz(G) but I get the following error repeated for all nodes in the graph:

Error: node 1, position [0.127506302389087, 0.3262608552621944], expected two doubles

Then trying to figure a solution I used trial 2 A=nx.to_agraph(G) to get a pygraphviz graph but when I try to draw with nx.draw_graphviz(A) I get the following Error:

AttributeError: 'AGraph' object has no attribute 'number_of_selfloops'

nx.graphviz_layout but it gives back a dictionary of positions keyed by nodes, and I do not know how to use it to draw the graphviz layout.

Note I imported graphviz, pygraphviz and pydot modules but I do not know which are the ones really needed for this, but it still does not work.

Is there something missing there to draw the networkx graph in graphviz layout ?

Full Trace of errors for

Trial 1

Traceback (most recent call last):
  File "main.py", line 102, in <module>
    d.display_graph(sub_normal,NEUTRAL_RANGE)
  File "/home/abdallah/stage/Reputation_system/display.py", line 33, in display_graph
    nx.draw_graphviz(G)
  File "/usr/local/lib/python2.7/dist-packages/networkx-1.10-py2.7.egg/networkx/drawing/nx_pylab.py", line 982, in draw_graphviz
    pos = nx.drawing.graphviz_layout(G, prog)
  File "/usr/local/lib/python2.7/dist-packages/networkx-1.10-py2.7.egg/networkx/drawing/nx_pydot.py", line 257, in graphviz_layout
    return pydot_layout(G=G,prog=prog,root=root,**kwds)
  File "/usr/local/lib/python2.7/dist-packages/networkx-1.10-py2.7.egg/networkx/drawing/nx_pydot.py", line 277, in pydot_layout
    D=P.create_dot(prog=prog)
  File "/usr/lib/python2.7/dist-packages/pydot.py", line 1802, in <lambda>
    lambda f=frmt, prog=self.prog : self.create(format=f, prog=prog))
  File "/usr/lib/python2.7/dist-packages/pydot.py", line 2023, in create
    status, stderr_output) )
pydot.InvocationException: Program terminated with status: 1. stderr follows: Error: node 0, position [0.7145101895899024, 0.9016482786797262], expected two doubles

trial 2

Traceback (most recent call last):
  File "main.py", line 102, in <module>
    d.display_graph(sub_normal,NEUTRAL_RANGE)
  File "/home/abdallah/stage/Reputation_system/display.py", line 33, in display_graph
    nx.draw_graphviz(A)
  File "/usr/local/lib/python2.7/dist-packages/networkx-1.10-py2.7.egg/networkx/drawing/nx_pylab.py", line 982, in draw_graphviz
    pos = nx.drawing.graphviz_layout(G, prog)
  File "/usr/local/lib/python2.7/dist-packages/networkx-1.10-py2.7.egg/networkx/drawing/nx_pydot.py", line 257, in graphviz_layout
    return pydot_layout(G=G,prog=prog,root=root,**kwds)
  File "/usr/local/lib/python2.7/dist-packages/networkx-1.10-py2.7.egg/networkx/drawing/nx_pydot.py", line 273, in pydot_layout
    P=to_pydot(G)
  File "/usr/local/lib/python2.7/dist-packages/networkx-1.10-py2.7.egg/networkx/drawing/nx_pydot.py", line 192, in to_pydot
    strict=N.number_of_selfloops()==0 and not N.is_multigraph()
AttributeError: 'AGraph' object has no attribute 'number_of_selfloops'
Abdallah Sobehy
  • 2,881
  • 1
  • 15
  • 28
  • What versions of python and networkx? – Joel Sep 15 '15 at 14:20
  • Python 2.7.6, as for Networkx I suppose it is 1.9.1 (but I am not sure because I do not know how to get it ) – Abdallah Sobehy Sep 15 '15 at 14:27
  • So far I don't have any good ideas. Can you give the full traceback for the errors? – Joel Sep 15 '15 at 14:37
  • I edited the question to include the Full trace back errors in both cases, Trial 1 and trial 2 (where I used nx.to_agraph function) – Abdallah Sobehy Sep 15 '15 at 14:46
  • I am not sure what did I do, but when I tried the draw_graphviz function the next day it worked ! (even without importing pydot, graphviz, pygraphviz modules) May be some changes like installations took effect after restarting. So, am I suuposed to delete the question ? or just leave it there ? @Joel – Abdallah Sobehy Sep 17 '15 at 08:07
  • 1
    This makes it a "can no longer be reproduced" problem. So it should be closed. You should delete it. – Joel Sep 17 '15 at 10:07
  • Sorry to bring it again but it seems that the problem can be reproduced because when I tried `G = nx.random_geometric_graph(50,0.25); nx.draw_graphviz(G, with_labels=True); plt.show()` in ipython notebook it gave me an error similar to the one in Trial 1. So, I will leave it there maybe someone can figure out a solution for it. – Abdallah Sobehy Sep 22 '15 at 15:33

1 Answers1

0

I used graphviz_layout and i succeed, here is my Python example:

try:
    import pygraphviz
    from networkx.drawing.nx_agraph import graphviz_layout
except ImportError:
    try:
        import pydotplus
        from networkx.drawing.nx_pydot import graphviz_layout
    except ImportError:
        raise ImportError("This example needs Graphviz and either "
                              "PyGraphviz or PyDotPlus")
import networkx as nx
plt.figure(figsize=(6,8))
pos=graphviz_layout(G)
nx.draw_networkx_nodes(G,pos,nodelist=G.nodes(),node_size=node_sizes,\
linewidths=0.1,vmin=0,vmax=1,alpha=0.8,\
node_color=[D[n] for n in G.nodes()])
nx.draw_networkx_edges(G,pos,edgelist=G.edges(),width=0.1,\
edge_color="black",alpha=0.6)
plt.axis('off')
plt.tight_layout()
plt.show()

Here is another example using graphviz_layout from Networkx website

https://networkx.github.io/documentation/networkx-1.10/examples/drawing/lanl_routes.html

Odin
  • 1,914
  • 10
  • 5