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

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

问题描述

我正在学习一些Python图形可视化。我发现一些博客文章在一些 东西我想试试。不幸的是,我没有太多,遇到这个错误: 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(最近一次调用最后一次)
& lt; ipython-input-3-481ad1c1771c& gt; in& lt; module& gt;()
----& gt; 1 draw_graphviz(G,prog,** kwargs)
/usr/lib/python2.7/site-packages/networkx/drawing/nx_pylab.pyc
982请参阅networkx。 draw_networkx()用于描述可选关键字。
983
- & gt; 984 pos = nx.drawing.graphviz_layout(G,prog)
绘制(G,pos,** kwargs)
986
AttributeError:'module'object has no attribute'graphviz_layout'

我发现了一个类似问题文章对这个组合有困难,但没有完全一样的错误,其中一个是 close ,但它会自动解析。

://networkx.readthedocs.org/en/stable/install.html#requirementsrel =nofollow noreferrer> NetworkX 和PyGraphViz(wh我们列出了与 Scipy 类似的要求)。



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



MatPlotLib代码示例适用于我(将图像呈现在屏幕上)

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

然而, GraphViz剪辑也会产生类似的错误,

 <$ c $在[16]中:import networkx as nx 
In [17]:G = nx.complete_graph(5)
In [18]:H = nx.from_agraph(A)
- -------------------------------------------------- ---------
AttributeError Traceback(最近一次调用最后一次)
< ipython-input-18-808fa68cefaa> in< module>()
----> 1 H = nx.from_agraph(A)
AttributeError:'模块'对象没有属性'from_agraph'
In [19]:A = nx.to_agraph(G)
---- -------------------------------------------------- ------
AttributeError Traceback(最近一次调用最后一次)
< ipython-input-19-32d1616bb41a> in< module>()
----> 1 A = nx.to_agraph(G)
AttributeError:'module'对象没有属性'to_agraph'
In [20]:print G
complete_graph(5)

然后我在。 io / documentation / development / tutorial.html#layout-and-drawingrel =nofollow noreferrer> Layout&绘图。这也有一些剪辑。 PyGraphViz通过与Neato(默认),PyDot和Circo Post Script输出(使用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()
在[6]中:A.draw('pygraphviz_test_01.png')

增加了复杂性, PyGraphViz 需要GraphViz 软件包二进制文件才能工作。我正在使用Arch Linux,并安装了该发行版。 Arch Linux有一个示例来测试安装(再次输出到文件)这也通过



我错过了什么? 可能导致NetworkX& PyGraphViz可以很好地独立工作,但不能一起工作吗?

解决方案

通过graphviz绘图工具不再导入到networkx的顶级命名空间中的更改触发了networkx-1.11中的> draw_graphviz 函数。



以下是解决方法:

  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)
$ b $

要使用其他函数,如 to_agraph write_dot 等你需要明确地使用较长的路径名称

$ $ p $ code> nx.drawing.nx_agraph.write_dot()

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

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


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)
&lt;ipython-input-3-481ad1c1771c&gt; in &lt;module&gt;()
----&gt; 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 """
--&gt; 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.

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

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.

The MatPlotLib code example works for me (renders an image to the screen),

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()  

However, the GraphViz snips also produce similar errors,

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)

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')

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.

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

解决方案

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.

The following is a workaround

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 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()

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

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