在Python中无法可视化网络 - Pygraphviz存在问题?

4
当我使用一些代码来可视化网络时,遇到了一个错误。
Traceback (most recent call last):
File "networkplot.py", line 21, in <module>
pos = graphviz_layout(G, prog='neato')
File "/opt/conda/lib/python2.7/site-packages/networkx/drawing/nx_agraph.py", line 228, in graphviz_layout
return pygraphviz_layout(G,prog=prog,root=root,args=args)
File "/opt/conda/lib/python2.7/site-packages/networkx/drawing/nx_agraph.py", line 258, in pygraphviz_layout
'http://pygraphviz.github.io/')
ImportError: ('requires pygraphviz ', 'http://pygraphviz.github.io/')

我因此下载了pygraphiviz-1.3.1.tar.gzpip install pygraphviz。它显示了:
Failed building wheel for pygraphviz
Running setup.py clean for pygraphviz
Failed to build pygraphviz
Installing collected packages: pygraphviz
Running setup.py install for pygraphviz ... error
Complete output from command /opt/conda/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-0RJB9q/pygraphviz/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-vo59d4-record/install-record.txt --single-version-externally-managed --compile:
running install
Trying pkg-config
Package libcgraph was not found in the pkg-config search path.
Perhaps you should add the directory containing `libcgraph.pc'
to the PKG_CONFIG_PATH environment variable
No package 'libcgraph' found
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/tmp/pip-build-0RJB9q/pygraphviz/setup.py", line 87, in <module>
    tests_require=['nose>=0.10.1', 'doctest-ignore-unicode>=0.1.0',],
  File "/opt/conda/lib/python2.7/distutils/core.py", line 151, in setup
    dist.run_commands()
  File "/opt/conda/lib/python2.7/distutils/dist.py", line 953, in run_commands
    self.run_command(cmd)
  File "/opt/conda/lib/python2.7/distutils/dist.py", line 972, in run_command
    cmd_obj.run()
  File "setup_commands.py", line 44, in modified_run
    self.include_path, self.library_path = get_graphviz_dirs()
  File "setup_extra.py", line 121, in get_graphviz_dirs
    include_dirs, library_dirs = _pkg_config()
  File "setup_extra.py", line 44, in _pkg_config
    output = S.check_output(['pkg-config', '--libs-only-L', 'libcgraph'])
  File "/opt/conda/lib/python2.7/subprocess.py", line 573, in check_output
    raise CalledProcessError(retcode, cmd, output=output)
subprocess.CalledProcessError: Command '['pkg-config', '--libs-only-L', 'libcgraph']' returned non-zero exit status 1

----------------------------------------
Command "/opt/conda/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-0RJB9q/pygraphviz/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-vo59d4-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-build-0RJB9q/pygraphviz/

我使用的代码是:
import networkx as nx
import matplotlib.pyplot as plt
plt.switch_backend('agg')
from networkx.drawing.nx_agraph import graphviz_layout
import graphs 

A = graphs.create_graph()
graph = A.graph
G, labels = A.networkList()

fig = plt.figure()
pos = graphviz_layout(G, prog='neato')

请问有人可以告诉我如何解决这个问题吗?非常感谢您的帮助。非常感谢!


是的,我添加了 import graphviz,但仍然显示 ImportError: ('requires pygraphviz ', 'http://pygraphviz.github.io/') - yearntolearn
3个回答

1

libcgraphhttp://graphviz.org/ 的一部分。在安装 pygraphviz 之前,您需要先安装 graphviz 包。使用 pip 安装的 graphviz 包是一个 Python 包,而不是您所需的 graphviz 包。


你尝试将包含libcgraph的目录添加到PKG_CONFIG_PATH环境变量中了吗? - manan

1

打开一个新的 PowerShell
使用以下命令安装 pygraphviz:

conda install -c conda-forge pygraphviz

0

你可以在 Windows 中下载 Graphviz 并通过以下方式运行 neato

dot -Kneato -Tpng file_name.dot -o file_name.png

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接