python - What could cause NetworkX & PyGraphViz to work fine alone but not together? -
i'm working learning python graph visualization. found few blog posts doing some things wanted try. unfortunately didn't far, encountering error: attributeerror: 'module' object has no attribute 'graphviz_layout'
the simplest snip of code reproduces error on system this,
in [1]: import networkx nx in [2]: g=nx.complete_graph(5) in [3]: nx.draw_graphviz(g) ------------------------------------------------------------ attributeerror traceback (most recent call last) <ipython-input-3-481ad1c1771c> in <module>() ----> 1 nx.draw_graphviz(g) /usr/lib/python2.7/site-packages/networkx/drawing/nx_pylab.pyc in draw_graphviz(g, prog, **kwargs) 982 see networkx.draw_networkx() description of optional keywords. 983 """ --> 984 pos = nx.drawing.graphviz_layout(g, prog) 985 draw(g, pos, **kwargs) 986 attributeerror: 'module' object has no attribute 'graphviz_layout' i found similar questions, , posts having difficulty combo, not quite same error. 1 close, automagically resolved itself.
first, verified required packages networkx , pygraphviz (which lists similar requirements scipy) installed.
next, looked snips test installation of these modules in python. first 2 examples networkx reference documentation. lists few example snips using both matplotlib , graphviz.
the matplotlib code example works me (renders image screen),
in [11]: import networkx nx in [12]: g=nx.complete_graph(5) in [13]: import matplotlib.pyplot plt in [13]: nx.draw(g) in [13]: plt.show() however, graphviz snips produce similar errors,
in [16]: import networkx nx in [17]: g=nx.complete_graph(5) in [18]: h=nx.from_agraph(a) ------------------------------------------------------------ attributeerror traceback (most recent call last) <ipython-input-18-808fa68cefaa> in <module>() ----> 1 h=nx.from_agraph(a) attributeerror: 'module' object has no attribute 'from_agraph' in [19]: a=nx.to_agraph(g) ------------------------------------------------------------ attributeerror traceback (most recent call last) <ipython-input-19-32d1616bb41a> in <module>() ----> 1 a=nx.to_agraph(g) attributeerror: 'module' object has no attribute 'to_agraph' in [20]: print g complete_graph(5) then tried pygraphviz's tutorial page on layout & drawing. has snips well. pygraphviz passed neato (default), pydot, , circo post script output (viewed using gimp). (the difference these pygraphviz examples not rendered display, files).
in [1]: import pygraphviz pgv in [2]: d={'1': {'2': none}, '2': {'1': none, '3': none}, '3': {'2': none}} in [3]: a=pgv.agraph(d) in [4]: a.write("pygraphviz_test_01.dot") in [5]: a.layout() in [6]: a.draw('pygraphviz_test_01.png') adding complexity, pygraphviz requires graphviz package binaries in order work. i'm using arch linux, , installed distro's version. arch linux has example test installation (again, output file) which passed.
what missing? what cause networkx & pygraphviz work fine alone not together?
there small bug in draw_graphviz function in networkx-1.11 triggered change graphviz drawing tools no longer imported top level namespace of networkx.
the following workaround
in [1]: import networkx nx in [2]: g = nx.complete_graph(5) in [3]: networkx.drawing.nx_agraph import graphviz_layout in [4]: pos = graphviz_layout(g) in [5]: nx.draw(g, pos) to use other functions such to_agraph, write_dot, etc need explicitly use longer path name
nx.drawing.nx_agraph.write_dot() or import function top-level namespace
from networkx.drawing.nx_agraph import write_dot() write_dot()
Comments
Post a Comment