Python 3 networkx draw_graphviz() 不起作用
Python 3 networkx draw_graphviz() does not work
我只想用 Python 3 networkx & graphviz 画一个简单的图:
import networkx as nx
G = nx.complete_graph(3)
nx.draw_graphviz(G)
我正在使用 ubuntu14.04 和 IPython3 并且像往常一样我做了 pip3 install networkx 并且 运行 代码给了我:
ImportError: pydot could not be loaded: http://code.google.com/p/pydot/
我尝试安装 pydotplus 和 运行 代码:
/usr/local/lib/python3.4/dist-packages/networkx/drawing/nx_pydot.py in pydot_layout(G, prog, root, **kwds)
294
295 if isinstance(node,list):
--> 296 node=node[0]
297 pos=node.get_pos()[1:-1] # strip leading and trailing double quotes
298 if pos != None:
IndexError: list index out of range
和 pydot2 也:
/usr/local/lib/python3.4/dist-packages/pydot.py in write(self, path, prog, format)
1893 prog = self.prog
1894
-> 1895 dot_fd = file(path, "w+b")
1896 if format == 'raw':
1897 data = self.to_string()
NameError: name 'file' is not defined
我已经花了很多时间搜索和安装其他 pydots 和 pygraphviz 组合,但仍然没有运气。
虽然这可能是相关的:
pydot and graphviz error: Couldn't import dot_parser, loading of dot files will not be possible,这并没有解决 Python 3.
中的问题
这不是一个很好的答案,但它可以作为一种解决方法。
首先输出.dot文件
networkx.write_dot(G, 'graph.dot')
使用 Python
然后在命令行上执行适当的 graphviz 输出命令,如 neato -T png graph.dot > graph.png
。
您可以通过编辑行 #292
来解决这个问题:
pydot_node = pydot.Node(make_str(n)).get_name().encode('utf-8')
删除末尾的编码:
pydot_node = pydot.Node(make_str(n)).get_name() #.encode('utf-8')
我已经举报了 bug/fix here。
这似乎与您使用的 pydot 的版本与使用 file(...) 的 Python 3 不兼容是同一个问题。 file(...) 已在 Python 3 中删除。
我注意到这个问题并在 PyPi 上安装了 Python 3 兼容版本。
对于 Python 3.x 的 Linux 系统,尝试:
pip3 install pydot3
或者一般来说 Python 2.x,试试:
pip install pydot3
我只想用 Python 3 networkx & graphviz 画一个简单的图:
import networkx as nx
G = nx.complete_graph(3)
nx.draw_graphviz(G)
我正在使用 ubuntu14.04 和 IPython3 并且像往常一样我做了 pip3 install networkx 并且 运行 代码给了我:
ImportError: pydot could not be loaded: http://code.google.com/p/pydot/
我尝试安装 pydotplus 和 运行 代码:
/usr/local/lib/python3.4/dist-packages/networkx/drawing/nx_pydot.py in pydot_layout(G, prog, root, **kwds)
294
295 if isinstance(node,list):
--> 296 node=node[0]
297 pos=node.get_pos()[1:-1] # strip leading and trailing double quotes
298 if pos != None:
IndexError: list index out of range
和 pydot2 也:
/usr/local/lib/python3.4/dist-packages/pydot.py in write(self, path, prog, format)
1893 prog = self.prog
1894
-> 1895 dot_fd = file(path, "w+b")
1896 if format == 'raw':
1897 data = self.to_string()
NameError: name 'file' is not defined
我已经花了很多时间搜索和安装其他 pydots 和 pygraphviz 组合,但仍然没有运气。
虽然这可能是相关的: pydot and graphviz error: Couldn't import dot_parser, loading of dot files will not be possible,这并没有解决 Python 3.
中的问题这不是一个很好的答案,但它可以作为一种解决方法。
首先输出.dot文件
networkx.write_dot(G, 'graph.dot')
使用 Python
然后在命令行上执行适当的 graphviz 输出命令,如 neato -T png graph.dot > graph.png
。
您可以通过编辑行 #292
来解决这个问题:
pydot_node = pydot.Node(make_str(n)).get_name().encode('utf-8')
删除末尾的编码:
pydot_node = pydot.Node(make_str(n)).get_name() #.encode('utf-8')
我已经举报了 bug/fix here。
这似乎与您使用的 pydot 的版本与使用 file(...) 的 Python 3 不兼容是同一个问题。 file(...) 已在 Python 3 中删除。
我注意到这个问题并在 PyPi 上安装了 Python 3 兼容版本。
对于 Python 3.x 的 Linux 系统,尝试:
pip3 install pydot3
或者一般来说 Python 2.x,试试:
pip install pydot3