什么可能导致 NetworkX &PyGraphViz 可以单独工作但不能一起工作吗? [英] What could cause NetworkX & PyGraphViz to work fine alone but not together?

查看:15
本文介绍了什么可能导致 NetworkX &PyGraphViz 可以单独工作但不能一起工作吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm working to learning some Python graph visualization. I found a few blog posts doing some things I wanted to try. Unfortunately I didn't get too far, encountering this error: AttributeError: 'module' object has no attribute 'graphviz_layout'

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

The simplest snip of code which reproduces the error on my system is this,

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'

我发现了一个类似的问题,并且帖子有困难使用这个组合,但不完全相同的错误.一个是关闭,但它会自动自行解决.

I found a similar questions, and posts having difficulty with this combo, but not quite the same error. One was close, but it automagically resolved itself.

首先,我验证了 的所有必需软件包NetworkX 和 PyGraphViz(列出了与 Scipy) 已安装.

First, I verified all the required packages for NetworkX and PyGraphViz (which lists similar requirements to Scipy) were installed.

接下来,我寻找片段来测试我在 Python 中安装这些模块的情况.前两个示例来自 NetworkX 参考文档.这列出了一些使用 MatPlotLib 和 GraphViz 的示例剪辑.

Next, I looked for snips to test my installation of these modules in Python. The first two examples are from the NetworkX Reference Documentation. This lists a few example snips using both MatPlotLib and 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 snips 也会产生类似的错误,

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)

然后我在 ">布局&绘图.这也有一些剪辑.PyGraphViz 通过 Neato(默认)、PyDot 和 Circo Post Script 输出(使用 Gimp 查看).(唯一的区别是这些 PyGraphViz 示例不会渲染到显示器,而是渲染到文件).

Then I tried PyGraphViz's tutorial page on Layout & Drawing. This has some snips as well. PyGraphViz passed with Neato (default), PyDot, and Circo Post Script output (viewed using Gimp). (The only difference is these PyGraphViz examples are not rendered to the display, but to files).

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 需要 GraphViz 包二进制文件才能工作.我正在使用 Arch Linux,并安装了该发行版的版本.Arch Linux 有一个示例来测试安装(同样,输出到文件)也通过了.

Adding to the complexity, 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 (again, output to file) which also passed.

我错过了什么?什么可能导致 NetworkX &PyGraphViz 可以单独工作但不能一起工作吗?

推荐答案

networkx-1.11 中的draw_graphviz 函数有一个小bug,是由于graphviz 绘图工具不再导入而触发的进入 networkx 的顶级命名空间.

There is a small bug in the draw_graphviz function in networkx-1.11 triggered by the change that the graphviz drawing tools are no longer imported into the top level namespace of 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 等,您需要明确使用较长的路径名

To use the other functions such as to_agraph, write_dot, etc you will need to explicitly use the longer path name

 nx.drawing.nx_agraph.write_dot()

或将函数导入顶级命名空间

or import the function into the top-level namespace

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

这些命令使用 graphviz 来计算节点的位置 (graphviz_layout 使用 neato 默认)和matplotlib 实际上 绘制(即绘制) 图形.

These command use graphviz to calculate the positioning of the nodes (graphviz_layout uses neato per default), and matplotlib to actually plot (i.e. draw) the graph.

这篇关于什么可能导致 NetworkX &amp;PyGraphViz 可以单独工作但不能一起工作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆