Python NetworkX错误:模块'networkx.drawing'没有属性'graphviz_layout'

时间:2016-12-08 19:29:57

标签: python nltk graphviz networkx

我正在使用“使用Python进行自然语言处理”(“www.nltk.org/book”)这本书教自己Python和NLTK。

我被困在NetworkX的第4章第4部分第8部分。当我尝试运行示例4.15时,它应绘制图形,但我得到以下错误消息:

  

AttributeError:模块'networkx.drawing'没有属性'graphviz_layout'

罪魁祸首代码行似乎是

>>> nx.draw_graphviz(graph,
    node_size = [16 * graph.degree(n) for n in graph],
    node_color = [graph.depth[n] for n in graph],
    with_labels = False)

这是从“networkx.github.io/documentation/networkx-1.10/tutorial/tutorial.html”中借用的简化代码

>>> import networkx as nx
>>> import matplotlib.pyplot as plt
>>> G=nx.Graph()
>>> G.add_node(1)
>>> G.add_nodes_from([2,3])
>>> nx.draw_graphviz(G)  
Traceback (most recent call last):
  File "<pyshell#92>", line 1, in <module>
    nx.draw_graphviz(G)
  File "C:\Users\Cheese\AppData\Local\Programs\Python\Python35-32\lib\site-packages\networkx\drawing\nx_pylab.py", line 984, in draw_graphviz
    pos = nx.drawing.graphviz_layout(G, prog)
AttributeError: module 'networkx.drawing' has no attribute 'graphviz_layout'
>>> 

你能告诉我如何解决这个问题吗?

我使用的是Python 7家庭高级版,使用Python 3.5,Graphviz2.38(并且该目录位于PATH环境变量中)和NetworkX 1.11。

我已多次搜索,无法找到适合我的任何内容。我浏览了NetworkX和graphviz教程,这也没有帮助。

以下是我发现没有帮助的内容:

“stackoverflow.com/questions/39411102/attributeerror-module-object-has-no-attribute-graphviz-layout-with-networkx”(答案代码给了我同样的错误信息)

“python.thenaiveapproach.com/buggy-module-installation-networkx-pygraphviz/”(每个错误消息,需要pygraphviz,我无法安装.pip说它需要Visual C ++才能运行,我不能在我的工作计算机上安装它。)

“codedump.io/share/c3aAbCneu2oA/1/attributeerror-39module39-object-has-no-attribute-39graphvizlayout39-with-networkx-111”(还需要pygraphviz - 见上文)

非常感谢, 珍

1 个答案:

答案 0 :(得分:1)

答案是@Bonlenfum礼貌和来自https://networkx.github.io/documentation/networkx-1.10/examples/drawing/simple_path.html的代码

以下是有效的代码:

>>> import networkx as nx
>>> import matplotlib.pyplot as plt
>>> G=nx.Graph()
>>> G.add_node(1)
>>> G.add_nodes_from([2,3])
>>> nx.draw(G)
>>> plt.savefig("simple_path.png")
>>> plt.show()

这是NLTK书中调整后的代码:

try:
    import matplotlib.pyplot as plt
except:
    raise

def graph_draw(graph):
    nx.draw(graph,
         node_size = [16 * graph.degree(n) for n in graph],
         node_color = [graph.depth[n] for n in graph],
         with_labels = False)