2

我在pygraph 的文档中找到了如何更改节点和边的属性,但我没有找到关于如何更改图形属性的帮助。

我试过没有运气:

gr = graph()
gr.__setattr__('aspect',2)

你有什么建议吗?

谢谢!

[更新]我也试过:

gr = graph()
gr.__setattr__('rotate',90)
gr.rotate = 90
gr.color = 'red'
setattr(gr,'bgcolor','red')

[更新 2] 来自网站的示例代码,具有不同的想法来更改属性:

#!/usr/bin/env python

# Copyright (c) 2007-2008 Pedro Matiello <pmatiello@gmail.com>
# License: MIT (see COPYING file)

# Import graphviz
import sys
sys.path.append('..')
sys.path.append('/usr/lib/graphviz/python/')
sys.path.append('/usr/lib64/graphviz/python/')
import gv

# Import pygraph
from pygraph.classes.graph import graph
from pygraph.classes.digraph import digraph
from pygraph.algorithms.searching import breadth_first_search
from pygraph.readwrite.dot import write

# Graph creation
gr = graph()

gr.__setattr__('rotate',90)
gr.rotate = 90
gr.color = 'red'
setattr(gr,'bgcolor','red')

# Add nodes and edges
gr.add_nodes(["Portugal","Spain","France","Germany","Belgium","Netherlands","Italy"])
gr.add_nodes(["Switzerland","Austria","Denmark","Poland","Czech Republic","Slovakia","Hungary"])
gr.add_nodes(["England","Ireland","Scotland","Wales"])

gr.add_edge(("Portugal", "Spain"))
gr.add_edge(("Spain","France"))
gr.add_edge(("France","Belgium"))
gr.add_edge(("France","Germany"))
gr.add_edge(("France","Italy"))
gr.add_edge(("Belgium","Netherlands"))
gr.add_edge(("Germany","Belgium"))
gr.add_edge(("Germany","Netherlands"))
gr.add_edge(("England","Wales"))
gr.add_edge(("England","Scotland"))
gr.add_edge(("Scotland","Wales"))
gr.add_edge(("Switzerland","Austria"))
gr.add_edge(("Switzerland","Germany"))
gr.add_edge(("Switzerland","France"))
gr.add_edge(("Switzerland","Italy"))
gr.add_edge(("Austria","Germany"))
gr.add_edge(("Austria","Italy"))
gr.add_edge(("Austria","Czech Republic"))
gr.add_edge(("Austria","Slovakia"))
gr.add_edge(("Austria","Hungary"))
gr.add_edge(("Denmark","Germany"))
gr.add_edge(("Poland","Czech Republic"))
gr.add_edge(("Poland","Slovakia"))
gr.add_edge(("Poland","Germany"))
gr.add_edge(("Czech Republic","Slovakia"))
gr.add_edge(("Czech Republic","Germany"))
gr.add_edge(("Slovakia","Hungary"))

# Draw as PNG
dot = write(gr)
gvv = gv.readstring(dot)
gv.layout(gvv,'dot')
gv.render(gvv,'png','europe.png')
4

4 回答 4

3

显然,您可以使用gv.setv()在图表上设置属性。

import gv
g = gv.digraph('19261920')
gv.setv(g, 'overlap', 'compress')

gv.setv()通过检查gv.py接口定义文件发现/usr/include/graphviz/gv.i

在 interwebz 上搜索"import gv" overlap setv让我看到了这篇旧帖子,该帖子演示了gv.setv().

我发现这个PDF gv_python 手册页包含更多关于setv.

对于这么简单的事情,这是相当多的侦查,是吗?

于 2013-02-08T23:47:27.150 回答
1

所以,我自己做更多的研究,我发现pygraphviz提供了我需要的东西。这是pygraph使用pygraphviz和接受属性的示例。它也更短,因为如果所有节点都通过边连接,则不必指定节点。

#!/usr/bin/env python

# Copyright (c) 2007-2008 Pedro Matiello <pmatiello@gmail.com>
# License: MIT (see COPYING file)

# Import pygraphviz
import pygraphviz as pgv

# Graph creation and setting of attributes
gr = pgv.AGraph(rotate='90',bgcolor='lightgray')

# Add nodes and edges
gr.add_edge(("Portugal", "Spain"))
gr.add_edge(("Spain","France"))
gr.add_edge(("France","Belgium"))
gr.add_edge(("France","Germany"))
gr.add_edge(("France","Italy"))
gr.add_edge(("Belgium","Netherlands"))
gr.add_edge(("Germany","Belgium"))
gr.add_edge(("Germany","Netherlands"))
gr.add_edge(("England","Wales"))
gr.add_edge(("England","Scotland"))
gr.add_edge(("Scotland","Wales"))
gr.add_edge(("Switzerland","Austria"))
gr.add_edge(("Switzerland","Germany"))
gr.add_edge(("Switzerland","France"))
gr.add_edge(("Switzerland","Italy"))
gr.add_edge(("Austria","Germany"))
gr.add_edge(("Austria","Italy"))
gr.add_edge(("Austria","Czech Republic"))
gr.add_edge(("Austria","Slovakia"))
gr.add_edge(("Austria","Hungary"))
gr.add_edge(("Denmark","Germany"))
gr.add_edge(("Poland","Czech Republic"))
gr.add_edge(("Poland","Slovakia"))
gr.add_edge(("Poland","Germany"))
gr.add_edge(("Czech Republic","Slovakia"))
gr.add_edge(("Czech Republic","Germany"))
gr.add_edge(("Slovakia","Hungary"))

# Draw as PNG
gr.layout(prog='dot')
gr.draw('europe.png')
于 2011-04-13T15:37:15.443 回答
0

我不了解 pygraph,但我认为您在 python 中设置属性的方式是:

setattr(object, attr, value)
setattr(gr, 'aspect', 2)

我的意思是,您已经尝试过 gr.aspect = 2,对吗?

于 2011-04-12T17:21:39.857 回答
0

尝试以下

import pydot

dot = write(gr)
dotG = pydot.graph_from_dot_data(dot)
params = { 'rotate': 10, 'color': 'red'}
dotG.set_graph_defaults(**params)
dot = dotG.to_string()
于 2014-01-23T14:37:19.147 回答