Search code examples
python-3.xnetworkxgraphvizpydot

How to get graph coordinates (layout) with networkx using graphviz?


I have a graph with 3000 nodes. I am trying to use the pydot layout engine to find a more pleasing layout than the default networkx layout layout = nx.fruchterman_reingold_layout(G)

The example from networkx doc

G_tst = nx.complete_graph(4)
pos = nx.nx_pydot.pydot_layout(G_tst )
pos = nx.nx_pydot.pydot_layout(G_tst , prog='dot')

works just fine. However when I use my own graph pos = nx.nx_pydot.pydot_layout(G) I get a Type Error where it claims that G has the attibute name more than once.

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-72-1326868cc786> in <module>()
      1 
----> 2 pos = nx.nx_pydot.pydot_layout(G)
      3 nx.draw(G, pos=pos)

C:\Anaconda3\lib\site-packages\networkx\drawing\nx_pydot.py in pydot_layout(G, prog, root, **kwds)
    261     """
    262     import pydotplus
--> 263     P=to_pydot(G)
    264     if root is not None :
    265         P.set("root",make_str(root))

C:\Anaconda3\lib\site-packages\networkx\drawing\nx_pydot.py in to_pydot(N, strict)
    200     for n,nodedata in N.nodes_iter(data=True):
    201         str_nodedata=dict((k,make_str(v)) for k,v in nodedata.items())
--> 202         p=pydotplus.Node(make_str(n),**str_nodedata)
    203         P.add_node(p)
    204 

TypeError: __init__() got multiple values for argument 'name'

Here are the node attributes I do have:

`G.add_node(G.number_of_nodes(), 
           name=endNode.endWord, # string
           teaching_text=endNode.tt_corpus, # string
           definition=endNode.domainDef, # string
           search_string=endNode.searchKey_obj.search_key_str,
           name_len = len(endNode.endWord))` #int

Solution

  • I got the same error yesterday. I'm not 100 percent sure but it seems some internal variables conflict with your attribute "name". In my case, I change it to "name_" then it works.