1

我正在尝试使用 graph_tool 中的 graph_draw 方法。我在 site_packages 和文档中看到了对它的引用

./cgenv/lib/python2.6/site-packages/graph_tool/draw/__init__.py:   graph_draw
./cgenv/lib/python2.6/site-packages/graph_tool/draw/__init__.py:__all__ = ["graph_draw", "graphviz_draw", "fruchterman_reingold_layout",
./cgenv/lib/python2.6/site-packages/graph_tool/draw/__init__.py:    >>> gt.graph_draw(g, pos=pos, output="graph-draw-fr.pdf")
./cgenv/lib/python2.6/site-packages/graph_tool/draw/__init__.py:       gt.graph_draw(g, pos=pos, output="graph-draw-fr.png")
./cgenv/lib/python2.6/site-packages/graph_tool/draw/__init__.py:    >>> gt.graph_draw(g, pos=pos, output="graph-draw-arf.pdf")
./cgenv/lib/python2.6/site-packages/graph_tool/draw/__init__.py:       gt.graph_draw(g, pos=pos, output="graph-draw-arf.png")
./cgenv/lib/python2.6/site-packages/graph_tool/draw/__init__.py:            graph_draw(u, mivs, vertex_fillcolor=mivs)
./cgenv/lib/python2.6/site-packages/graph_tool/draw/__init__.py:    >>> gt.graph_draw(g, pos=pos, output="graph-draw-sfdp.pdf")
./cgenv/lib/python2.6/site-packages/graph_tool/draw/__init__.py:       gt.graph_draw(g, pos=pos, output="graph-draw-sfdp.png")
./cgenv/lib/python2.6/site-packages/graph_tool/draw/__init__.py:       http://www.mathematica-journal.com/issue/v10i1/graph_draw.html
./cgenv/lib/python2.6/site-packages/graph_tool/draw/__init__.py:            #graph_draw(u, pos)

但是当我尝试访问它时,它不存在......

>>> x=graph_tool.draw.GraphView
>>> x=graph_tool.draw.graph_draw()
Traceback (most recent call last):
  File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd_exec.py", line 3, in Exec
    exec exp in global_vars, local_vars
  File "<input>", line 1, in <module>
AttributeError: 'module' object has no attribute 'graph_draw'

我错过了什么吗?我应该如何访问这个?

谢谢!

4

2 回答 2

3

这似乎是一个导入问题......“graph_draw”函数是从“graph_tool.all”而不是“graph_tool”导入的

我实际上正在使用 OSX 10.10.4,当我像这样导入“graph_tool”模块时:

from graph_tool.all import *  

#create your graph object
g = Graph()

#add vertex 
vertex_1 = g.add_vertex() #here you create a vertex
vertex_2 = g.add_vertex() #here you create a vertex

#add edge
g.add_edge(vertex_1,vertex_2) #add an edge 

#draw you graph 
graph_draw(
    g,
    output="test.png"
)   

其作品 !

如果我遵循您提供的“导入示例”,您也可以尝试:

from graph_tool.all import graph_draw,Graph  

#create your graph object
g = Graph()

#add a vertex at least
g.add_vertex()

#draw you graph 
graph_draw(
    g,
    output_size=(200,200),
    output="test.png"
)   

请尝试在您的计算机上运行之前的代码之一,以仔细检查它是模块的导入问题还是依赖项/安装问题

此致

于 2015-07-03T14:55:41.633 回答
2

确保您已安装所有可选依赖项:pycairo、matplotlib 和 gtk3(以及 librsvg,如果您使用的是 macports)。

于 2015-01-29T14:58:09.347 回答