【问题标题】:Can't plot pomegranate graph (pygraphviz not found)无法绘制石榴图(未找到 pygraphviz)
【发布时间】:2019-05-06 22:03:28
【问题描述】:

我不明白发生了什么,但我似乎不再能够从 PyCharm 内部绘制石榴图。 我正在使用 conda 作为包管理器,并且已经完成了常规操作:

conda install graphviz
conda install python-graphviz

但每次我从 PyCharm 内部调用 model.plot() 我都会得到

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/Applications/PyCharm.app/Contents/helpers/pydev/_pydev_bundle/pydev_umd.py", line 198, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "/Applications/PyCharm.app/Contents/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/Users/.../xai/import.py", line 36, in <module>
    model.plot()
  File "pomegranate/BayesianNetwork.pyx", line 281, in pomegranate.BayesianNetwork.BayesianNetwork.plot
ValueError: must have pygraphviz installed for visualization

我显然已经尝试安装pygraphviz,但似乎没有任何区别

【问题讨论】:

  • 我也尝试过从外部 Python 控制台进行绘图,但我得到了同样的错误,所以问题似乎不是 PyCharm 所特有的
  • 还有什么快乐......?

标签: python graphviz pygraphviz pomegranate


【解决方案1】:

遇到了类似的问题,虽然 @Baumann 的解决方案对我不起作用,但安装 matplotlib 解决了我的问题(Win10 和 WSL 下的 python 3.6)。

pip 安装 matplotlib

建议的背景:BayesianNetwork.pyx 中的石榴代码使用相同的 try 语句捕获多个导入异常(参见下面的代码,github, latest commit f116357 也可用,在我的情况下,即使我安装了 pygraphviz 没有 @987654325 @ installed 导致引发令人沮丧的异常。

第 40 行以后:

try:
    import tempfile
    import pygraphviz
    import matplotlib.pyplot as plt
    import matplotlib.image
except ImportError:
    pygraphviz = None

然后从第 222 行开始:

if pygraphviz is not None:
    G = pygraphviz.AGraph(directed=True)

    for state in self.states:
        G.add_node(state.name, color='red')

    for parent, child in self.edges:
        G.add_edge(parent.name, child.name)

    if filename is None:
        with tempfile.NamedTemporaryFile() as tf:
            G.draw(tf.name, format='png', prog='dot')
            img = matplotlib.image.imread(tf.name)
            plt.imshow(img)
            plt.axis('off')
    else:
        G.draw(filename, format='pdf', prog='dot')

else:
    raise ValueError("must have pygraphviz installed for visualization")

【讨论】:

    【解决方案2】:

    我刚刚遇到了同样的问题。我解决了安装graphviz开发包的问题。在 openSUSE 存储库中,它被称为graphviz-devel

    它的描述是:

    graphviz-devel 包包含开发所需的所有内容 使用包括 man3 页面在内的 graphviz 库的程序。

    【讨论】:

      猜你喜欢
      • 2019-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-16
      • 2020-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多