How do you get networkx.graphviz_layout to work?

2019-08-08 15:45发布

I'm trying to compute layouts for a graph that I built using networkx; let's call the graph G. I'm using Javascript to actually render the graph, so all I need right now is to compute the positions of the nodes using a layout algorithm. (I don't want to use Javascript tools like d3 force layouts because the computations take awhile for large graphs and so I would rather do the heavy lifting server-side.)

Here's a line of code which works for me and leads to a successful graph visualization:

layout = networkx.spring_layout(G)

However, this algorithm is both slow and not very pretty. So I thought I would try Graphviz:

layout = networkx.graphviz_layout(G)

I'm running Python 3.4 in OSX, so I first installed Graphviz using:

brew install graphviz
pip install graphviz
pip install pydotplus

(I first tried installing the pydot package instead of pydotplus, but it seems that the pydot package does not support Python 3.)

When I try to call graphviz_layout I end up with:

AttributeError: 'NoneType' object has no attribute 'get_node'

triggered by the line:

node=Q.get_node(pydot_node)

in nx_pydot.py. I'm sort of hoping that this is a known bug which someone can tell me how to fix, but I haven't found anything so far. In case nobody knows a simple fix I will not actively discourage people from recommending other Python packages which compute (but not necessarily render) graph layouts which support large graphs, though it seems that it is illegal for me to explicitly ask for this...

1条回答
太酷不给撩
2楼-- · 2019-08-08 16:31

The problem (in my case) was that I was binding a lot of data to the nodes in my networkx object, and some of the data was causing trouble for pydot. I was able to resolve the problem by stripping out these data for the purposes of computing the layout. This was good enough for my needs, but if somebody feels like tracking down the offending data in more detail I will accept their answer.

查看更多
登录 后发表回答