什么会导致 NetworkX 和 PyGraphViz 单独工作但不能一起工作?

What could cause NetworkX & PyGraphViz to work fine alone but not together?

我正在努力学习一些 Python 图形可视化。我发现一些博客文章 some things 我想试试。不幸的是我没有走得太远,遇到了这个错误:AttributeError: 'module' object has no attribute 'graphviz_layout'

在我的系统上重现错误的最简单的代码片段是这样的,

In [1]: import networkx as nx
In [2]: G=nx.complete_graph(5)
In [3]: nx.draw_graphviz(G)
------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-3-481ad1c1771c> in <module>()
----> 1 nx.draw_graphviz(G)
/usr/lib/python2.7/site-packages/networkx/drawing/nx_pylab.pyc in draw_graphviz(G, prog, **kwargs)
982 See networkx.draw_networkx() for a description of optional keywords.
983 """
--> 984 pos = nx.drawing.graphviz_layout(G, prog)
985 draw(G, pos, **kwargs)
986
AttributeError: 'module' object has no attribute 'graphviz_layout'

我找到了一个类似的 questions, and posts having difficulty with this combo, but not quite the same error. One was close,但它自动解决了。

首先,我验证了 NetworkX and PyGraphViz (which lists similar requirements to Scipy) 所需的所有软件包 都已安装。

接下来,我在 Python 中寻找片段来测试我对这些模块的安装。 前两个示例来自 NetworkX Reference Documentation。这列出了一些使用 MatPlotLib 和 GraphViz 的示例片段。

MatPlotLib 代码示例对我有用(将图像呈现到屏幕)

In [11]: import networkx as nx
In [12]: G=nx.complete_graph(5)
In [13]: import matplotlib.pyplot as plt
In [13]: nx.draw(G)
In [13]: plt.show()  

然而,GraphViz 片段也会产生类似的错误,

In [16]: import networkx as nx
In [17]: G=nx.complete_graph(5)
In [18]: H=nx.from_agraph(A)
------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-18-808fa68cefaa> in <module>()
----> 1 H=nx.from_agraph(A)
AttributeError: 'module' object has no attribute 'from_agraph'
In [19]: A=nx.to_agraph(G)
------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-19-32d1616bb41a> in <module>()
----> 1 A=nx.to_agraph(G)
AttributeError: 'module' object has no attribute 'to_agraph'
In [20]: print G
complete_graph(5)

然后我在 Layout & Drawing 上尝试了 PyGraphViz 的教程页面。这也有一些片段。 PyGraphViz 通过 Neato(默认)、PyDot 和 Circo Post 脚本输出(使用 Gimp 查看)。 (唯一的区别是这些 PyGraphViz 示例不是呈现给显示器,而是呈现给文件)。

In [1]: import pygraphviz as pgv
In [2]: d={'1': {'2': None}, '2': {'1': None, '3': None}, '3': {'2': None}}
In [3]: A=pgv.AGraph(d)
In [4]: A.write("pygraphviz_test_01.dot")
In [5]: A.layout()
In [6]: A.draw('pygraphviz_test_01.png')

增加了复杂性, PyGraphViz requires GraphViz package binaries in order to work. I'm using Arch Linux, and installed that distro's version. Arch Linux has an example to test installation(同样,输出到文件)也通过了

我错过了什么? 什么会导致 NetworkX 和 PyGraphViz 单独工作但不能一起工作?

networkx-1.11 中的 draw_graphviz 函数中存在一个小错误,这是由 graphviz 绘图工具不再导入到 networkx 的顶级命名空间的更改触发的。

以下是解决方法

In [1]: import networkx as nx

In [2]: G = nx.complete_graph(5)

In [3]: from networkx.drawing.nx_agraph import graphviz_layout

In [4]: pos = graphviz_layout(G)

In [5]: nx.draw(G, pos)

要使用 to_agraphwrite_dot 等其他函数,您需要明确使用较长的路径名

 nx.drawing.nx_agraph.write_dot()

或者将函数导入顶层命名空间

from networkx.drawing.nx_agraph import write_dot()
write_dot()

这些命令使用graphviz来计算节点的定位(graphviz_layout uses neato per default), and matplotlib to actually plot (i.e. draw) 图形。

nx.nx_pydot.graphviz_layout() 似乎是在 Networkx 版本 1.11.

中使用 graphviz_layout 的方式

您可能需要通过

安装 Graphviz
sudo apt-get install graphviz

文档中的一些示例代码:

import networkx as nx
G = nx.complete_graph(4)
pos = nx.nx_pydot.graphviz_layout(G)

我建议将其作为 Graphviz_layout documentation for 1.11