问题描述
我不知道发生了什么,但我似乎不再能够从PyCharm内部绘制石榴图.我将conda用作程序包管理器,但已经通过了通常的操作:
I can't understand what is going on but I no longer seem to be able to plot a pomegranate graph from inside PyCharm.I'm using conda as package manager and have gone though the usual:
conda install graphviz
conda install python-graphviz
但是每次我从PyCharm内部调用 model.plot()
时都会得到
but every time I call model.plot()
from inside PyCharm I get
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
,但似乎没有什么作用
I have obviously already tried installing pygraphviz
but it seems to make no difference
推荐答案
遇到了类似的问题,尽管@Baumann的解决方案对我不起作用,但安装 matplotlib
可以解决我的问题(在Python 3.6下Win10和WSL).
Faced a similar problem and while the solution from @Baumann did not work for me, installing matplotlib
fixed my problem (python 3.6 under Win10 and WSL).
建议的背景: BayesianNetwork.pyx
中的石榴代码使用相同的try语句捕获多个导入异常(请参见下面的代码,该代码也可在 github,最新提交f116357 ,就我而言,即使我安装了 pygraphviz
安装的 matplotlib
导致令人沮丧的异常出现.
Background for suggesting this: the pomegranate code in BayesianNetwork.pyx
catches multiple imports exceptions with the same try statement (see code below available also on github, latest commit f116357 and in my case even though i had installed pygraphviz
not having matplotlib
installed was resulting in the frustrating exception being raised.
第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")
这篇关于无法绘制石榴图(未找到pygraphviz)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!