gpt4 book ai didi

python - 使用 nx.draw_graphviz 在 python 中的 graphviz 布局中绘制图形会出错

转载 作者:太空狗 更新时间:2023-10-30 01:37:48 32 4
gpt4 key购买 nike

我正在尝试在 python 的 networkx 的 graphviz 布局中绘制一个 100 节点的多图 G,所以到目前为止我做了两次试验:

试验 1 nx.draw_graphviz函数如下 nx.draw_graphviz(G) 但我收到以下错误 repeated for all nodes in the graph:

Error: node 1, position [0.127506302389087, 0.3262608552621944], expected two doubles

然后试图找到一个解决方案,我使用trial 2 A=nx.to_agraph(G) 来获取 pygraphviz 图,但是当我尝试使用 绘制时nx.draw_graphviz(A) 我收到以下错误:

AttributeError: 'AGraph' object has no attribute 'number_of_selfloops'

nx.graphviz_layout但它返回一个由节点键控的位置字典,我不知道如何使用它来绘制 graphviz 布局。

注意 我导入了 graphviz、pygraphviz 和 pydot 模块,但我不知道哪些是真正需要的,但它仍然不起作用。

在 graphviz 布局中绘制 networkx 图是否缺少某些东西?

错误的完整跟踪

试验 1

Traceback (most recent call last):
File "main.py", line 102, in <module>
d.display_graph(sub_normal,NEUTRAL_RANGE)
File "/home/abdallah/stage/Reputation_system/display.py", line 33, in display_graph
nx.draw_graphviz(G)
File "/usr/local/lib/python2.7/dist-packages/networkx-1.10-py2.7.egg/networkx/drawing/nx_pylab.py", line 982, in draw_graphviz
pos = nx.drawing.graphviz_layout(G, prog)
File "/usr/local/lib/python2.7/dist-packages/networkx-1.10-py2.7.egg/networkx/drawing/nx_pydot.py", line 257, in graphviz_layout
return pydot_layout(G=G,prog=prog,root=root,**kwds)
File "/usr/local/lib/python2.7/dist-packages/networkx-1.10-py2.7.egg/networkx/drawing/nx_pydot.py", line 277, in pydot_layout
D=P.create_dot(prog=prog)
File "/usr/lib/python2.7/dist-packages/pydot.py", line 1802, in <lambda>
lambda f=frmt, prog=self.prog : self.create(format=f, prog=prog))
File "/usr/lib/python2.7/dist-packages/pydot.py", line 2023, in create
status, stderr_output) )
pydot.InvocationException: Program terminated with status: 1. stderr follows: Error: node 0, position [0.7145101895899024, 0.9016482786797262], expected two doubles

试验 2

Traceback (most recent call last):
File "main.py", line 102, in <module>
d.display_graph(sub_normal,NEUTRAL_RANGE)
File "/home/abdallah/stage/Reputation_system/display.py", line 33, in display_graph
nx.draw_graphviz(A)
File "/usr/local/lib/python2.7/dist-packages/networkx-1.10-py2.7.egg/networkx/drawing/nx_pylab.py", line 982, in draw_graphviz
pos = nx.drawing.graphviz_layout(G, prog)
File "/usr/local/lib/python2.7/dist-packages/networkx-1.10-py2.7.egg/networkx/drawing/nx_pydot.py", line 257, in graphviz_layout
return pydot_layout(G=G,prog=prog,root=root,**kwds)
File "/usr/local/lib/python2.7/dist-packages/networkx-1.10-py2.7.egg/networkx/drawing/nx_pydot.py", line 273, in pydot_layout
P=to_pydot(G)
File "/usr/local/lib/python2.7/dist-packages/networkx-1.10-py2.7.egg/networkx/drawing/nx_pydot.py", line 192, in to_pydot
strict=N.number_of_selfloops()==0 and not N.is_multigraph()
AttributeError: 'AGraph' object has no attribute 'number_of_selfloops'

最佳答案

我使用了 graphviz_layout 并成功了,这是我的 Python 示例:

try:
import pygraphviz
from networkx.drawing.nx_agraph import graphviz_layout
except ImportError:
try:
import pydotplus
from networkx.drawing.nx_pydot import graphviz_layout
except ImportError:
raise ImportError("This example needs Graphviz and either "
"PyGraphviz or PyDotPlus")
import networkx as nx
plt.figure(figsize=(6,8))
pos=graphviz_layout(G)
nx.draw_networkx_nodes(G,pos,nodelist=G.nodes(),node_size=node_sizes,\
linewidths=0.1,vmin=0,vmax=1,alpha=0.8,\
node_color=[D[n] for n in G.nodes()])
nx.draw_networkx_edges(G,pos,edgelist=G.edges(),width=0.1,\
edge_color="black",alpha=0.6)
plt.axis('off')
plt.tight_layout()
plt.show()

这是另一个使用 Networkx 网站上的 graphviz_layout 的例子

https://networkx.github.io/documentation/networkx-1.10/examples/drawing/lanl_routes.html

关于python - 使用 nx.draw_graphviz 在 python 中的 graphviz 布局中绘制图形会出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32587445/

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