我在documentation of pygraph中找到了如何更改attributes of the nodes and the edges,但我没有找到有关如何更改图表属性的帮助。
我没有运气试过:
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')
答案 0 :(得分: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
,让我看到this old post,演示了gv.setv()
的使用情况。
我发现此PDF gv_python man page包含有关setv
的更多文档。
对于这么简单的事情,这是一个很大的调查,嗯?
答案 1 :(得分: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')
答案 2 :(得分:0)
我不知道pygraph,但我认为你在python中设置属性的方式是:
setattr(object, attr, value)
setattr(gr, 'aspect', 2)
我的意思是,你试过gr.aspect = 2,对吧?
答案 3 :(得分: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()