gpt4 book ai didi

python - 属性错误 : 'module' object has no attribute 'graphviz_layout' with networkx 1. 11

转载 作者:IT老高 更新时间:2023-10-28 20:39:49 25 4
gpt4 key购买 nike

我正在尝试使用 networkx 1.11 绘制一些 DAG,但遇到了一些错误,这是测试:

import networkx as nx

print nx.__version__

G = nx.DiGraph()
G.add_node(1,level=1)
G.add_node(2,level=2)
G.add_node(3,level=2)
G.add_node(4,level=3)

G.add_edge(1,2)
G.add_edge(1,3)
G.add_edge(2,4)

import pylab as plt
nx.draw_graphviz(G, node_size=1600, cmap=plt.cm.Blues,
node_color=range(len(G)),
prog='dot')
plt.show()

这是回溯:

Traceback (most recent call last):
File "D:\sources\personal\python\framework\stackoverflow\test_dfs.py", line 69, in <module>
prog='dot')
File "d:\virtual_envs\py2711\lib\site-packages\networkx\drawing\nx_pylab.py", line 984, in draw_graphviz
pos = nx.drawing.graphviz_layout(G, prog)
AttributeError: 'module' object has no attribute 'graphviz_layout'

我正在使用 python 2.7.11 x64、networkx 1.11 并且我已经安装了 graphviz-2.38 dot 在 PATH 中可用。我错过了什么?

一旦它起作用,我怎么能用节点绘制图形:

  • 使用白色背景色
  • 里面有标签
  • 有方向箭头
  • 无论是自动还是手动都安排得很好

类似于下图的东西

enter image description here

正如您在该图像中看到的那样,节点对齐得非常好

最佳答案

在 networkx 的更高版本中,包布局已更改。您可以显式导入 graphivz_layout 函数。

import networkx as nx
import pylab as plt
from networkx.drawing.nx_agraph import graphviz_layout


G = nx.DiGraph()
G.add_node(1,level=1)
G.add_node(2,level=2)
G.add_node(3,level=2)
G.add_node(4,level=3)

G.add_edge(1,2)
G.add_edge(1,3)
G.add_edge(2,4)

nx.draw(G, pos=graphviz_layout(G), node_size=1600, cmap=plt.cm.Blues,
node_color=range(len(G)),
prog='dot')
plt.show()

enter image description here

关于python - 属性错误 : 'module' object has no attribute 'graphviz_layout' with networkx 1. 11,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39411102/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com