Edge between two subgraphs

41 views
Skip to first unread message

Alin Bastea

unread,
Aug 5, 2011, 4:44:35 AM8/5/11
to pydot
Hi,

Can anyone tell me how do I add in pydot an edge between two
subgraphs?

Thanks!

Kim Gräsman

unread,
Aug 10, 2011, 5:30:25 AM8/10/11
to py...@googlegroups.com
Hi Alin,

On Fri, Aug 5, 2011 at 10:44, Alin Bastea <alin...@gmail.com> wrote:
>
> Can anyone tell me how do I add in pydot an edge between two
> subgraphs?

This seems to work for me;

--
import pydot

g = pydot.Graph()
s1 = pydot.Cluster('first')
s2 = pydot.Cluster('second')

nodeA = pydot.Node('a')
nodeB = pydot.Node('b')
nodeC = pydot.Node('c')

s1.add_node(nodeA)
s1.add_node(nodeB)
s2.add_node(nodeC)

g.add_subgraph(s1)
g.add_subgraph(s2)
g.add_edge(pydot.Edge(nodeA, nodeB))
g.add_edge(pydot.Edge(nodeB, nodeC))

print(g.to_string())
--

I've previously assumed that edges in the parent graph can refer to
nodes within subgraphs, and DOT seems happy with rendering it, but I'm
not 100% sure it's allowed.

Note that you can create Edge objects based on the nodes name/id as
well, so you could do;

g.add_edge(pydot.Edge('a', 'b'))
g.add_edge(pydot.Edge('b', 'c'))

directly.

Hope that helps,
- Kim

Reply all
Reply to author
Forward
0 new messages