2

I found in the documentation of pygraph how to change the attributes of the nodes and the edges, but I found no help on how to change the attributes of the graphs.

I tried without luck:

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

What do you recommend?

Thanks!

[update] I also tried:

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

[update 2] Example code from the website with the different ideas to change the attributes:

#!/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')
Framester
  • 33,341
  • 51
  • 130
  • 192
  • 1
    @Framester: Those links do not point to the pygraph docs... – phooji Apr 12 '11 at 17:15
  • I know its not what you are asking for, but I like matplotlib. It has the same look and feel of math lab plots – Richard Apr 12 '11 at 17:16
  • @phooji - Thanks, I corrected the link. @Andrew - Thanks for the input. I just tried. It does not give an error, but the output is without change. :/ – Framester Apr 12 '11 at 17:20
  • What happens when you try to set these attributes? Does the interpreter return an error? If so, what is it? – JoshAdel Apr 12 '11 at 17:30
  • Hi Josh, the interpreter does not return an error. It runs through and prints the expected output disregarding the attributes. – Framester Apr 12 '11 at 17:39
  • @Framester: It looks like those are attributes of the graphviz object rather than the pygraph object. Plus the original example is http://code.google.com/p/python-graph/wiki/Example. It should be clear that they aren't trying to set the attributes as you have. – JoshAdel Apr 12 '11 at 17:47

4 Answers4

3

Apparently you can use gv.setv() to set attributes on a graph.

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

I found gv.setv() by examining the gv.py interface definition file at /usr/include/graphviz/gv.i

Searching the interwebz for "import gv" overlap setv led me to this old post which demonstrates use of gv.setv().

And I found this PDF gv_python man page which contains more documentation on setv.

That's quite a lot of sleuthing for such a simple thing, eh?

Inactivist
  • 9,997
  • 6
  • 29
  • 41
1

So, doing more research on my own, I found that pygraphviz is offering what I need. Here is the pygraph example using pygraphviz and accepting attributes. It is also shorter, as you don't have to specify the nodes if all of them are connected via edges.

#!/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')
Framester
  • 33,341
  • 51
  • 130
  • 192
0

Try the following

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()
Sankumarsingh
  • 9,889
  • 11
  • 50
  • 74
Cyrille Pontvieux
  • 2,356
  • 1
  • 21
  • 29
0

I don't know about pygraph, but I think the way you set attributes in python was:

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

I mean, you have tried gr.aspect = 2, right?

x10
  • 3,820
  • 1
  • 24
  • 32
  • Hi x10, thanks for this new idea. I tried both your ideas (see Q update), but the output stays the same. – Framester Apr 12 '11 at 17:27