Drawing graph in graphviz layout in python using nx.draw_graphviz gives error

帅比萌擦擦* 提交于 2020-01-03 12:24:12

问题


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'

回答1:


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



来源:https://stackoverflow.com/questions/32587445/drawing-graph-in-graphviz-layout-in-python-using-nx-draw-graphviz-gives-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!