當我嘗試在我的代碼中使用graphviz_layout方法時收到錯誤「InvocationException:找不到GraphViz的可執行文件,找不到GraphViz的可執行文件」。當graphviz_layout被spring_layout替換時,代碼工作正常。從返回異常信息看起來它調用pydot_layout。我通過Canopy的包管理器安裝了pydot模塊,但是導入它並沒有幫助。我也安裝graphviz安裝並導入它沒有運氣。在iPython筆記本中從NetworkX調用GraphViz佈局時出現「GraphViz的可執行文件未找到」
這裏是我的代碼:
import networkx as nx
import matplotlib.pyplot as plt
keywordTreeFile = open('decode_wordnet/keywordTreeFile.TXT','r') #keyword generation file
keywordTreeFileLineData = keywordTreeFile.readlines()
G = nx.Graph()
pairData = []
for i in range(0,len(keywordTreeFileLineData)):
pairData = pairData + [keywordTreeFileLineData[i].split('\t')]
pairData[i][1] = pairData[i][1].rstrip('\n')
G.add_edge(pairData[i][0],pairData[i][1])
pos = nx.graphviz_layout(G)
nx.draw(G,
pos=pos,
width = 1.0,
with_labels = True,
font_size = 3,
linewidths=.1
)
plt.savefig("graph.pdf")
這裏是倒退異常信息:
---------------------------------------------------------------------------
InvocationException Traceback (most recent call last)
<ipython-input-1-e85a11cf6191> in <module>()
15
16 #pos = nx.spring_layout(G,k=.15,iterations=50,scale=100)
---> 17 pos = nx.graphviz_layout(G)
18 #labels = nx.draw_networkx_labels(G,pos)
19 nx.draw(G,
/Users/scott/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/networkx/drawing/nx_pydot.pyc in graphviz_layout(G, prog, root, **kwds)
245 This is a wrapper for pydot_layout.
246 """
--> 247 return pydot_layout(G=G,prog=prog,root=root,**kwds)
248
249
/Users/scott/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/networkx/drawing/nx_pydot.pyc in pydot_layout(G, prog, root, **kwds)
269 P.set("root",make_str(root))
270
--> 271 D=P.create_dot(prog=prog)
272
273 if D=="": # no data returned
/Users/scott/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/pydot.pyc in <lambda>(f, prog)
1800 self.__setattr__(
1801 'create_'+frmt,
-> 1802 lambda f=frmt, prog=self.prog : self.create(format=f, prog=prog))
1803 f = self.__dict__['create_'+frmt]
1804 f.__doc__ = '''Refer to the docstring accompanying the 'create' method for more information.'''
/Users/scott/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/pydot.pyc in create(self, prog, format)
1951 if self.progs is None:
1952 raise InvocationException(
-> 1953 'GraphViz\'s executables not found')
1954
1955 if not self.progs.has_key(prog):
InvocationException: GraphViz's executables not found
系統信息:我使用的是最新版本,並Enthought冠層模塊的64位Mac上與OS X 10.9.5。另外,我正在使用iPython筆記本。