Cluster subgraph draw

1,798 views
Skip to first unread message

Guilhem Faure

unread,
May 5, 2009, 6:30:27 PM5/5/09
to pygraphvi...@googlegroups.com
Dear pygraphviz users,

I would like to build this kind of graph with pygraphviz : http://www.graphviz.org/Gallery/directed/cluster.html
If I write that :
>>> import pygraphviz as p
>>>
>>> G = p.AGraph(directed=True)
>>>
>>> G.add_node(1)
>>> G.add_node(2)
>>> G.add_node(3)
>>> G.add_node(4)
>>>
>>> G.add_edge(1,3)
>>>
>>> G1 = G.subgraph(nbunch=[1,2],name="cluster1")
>>> G2 = G.subgraph(nbunch=[3,4],name="cluster2")

When I draw the graph I have 2 box (fdp) -> it works

But if I use that in my case with more nodes (230nodes), it doesn't work. I can see my graph but without box.
clus1 = 117 nodes
clus2 = 113 nodes


Code :

    G = pgz.AGraph(directed=True)

    shaper = {"A":"triangle","B":"circle"}

    subA = []
    subB = []

   
    for c in contact:
       
        chain1,index1,aa1,chain2,index2,aa2 = c

        shape1 = shaper[chain1]
        shape2 = shaper[chain2]

        G.add_node(chain1+index1+aa1,shape=shape1)
        G.add_node(chain2+index2+aa2,shape=shape2)
       
       
        G.add_edge(chain1+index1+aa1,chain2+index2+aa2)

        if chain1 == "A":
            subA.append(chain1+index1+aa1)
        else:
            subB.append(chain1+index1+aa1)
        if chain2 == "A":
            subA.append(chain2+index2+aa2)
        else:
            subB.append(chain2+index2+aa2)
       
       
    C1 = G.subgraph(nbunch=list(set(subA)),name="clus1")
    C2 = G.subgraph(nbunch=list(set(subB)),name="clus2")
-> draw with fdp



My other question is, how I can access to kind of information for a subgraph
		style=filled;
color=lightgrey;
node [style=filled,color=white];
label = "cluster1"


Thank you for your help,

Regards,

Guilhem

Aric Hagberg

unread,
May 6, 2009, 8:54:16 AM5/6/09
to pygraphvi...@googlegroups.com

I think the name actually has to begin with "cluster" for
Graphviz to recognize it as a cluster.


>
> My other question is, how I can access to kind of information for a subgraph
>
> style=filled;
> color=lightgrey;
> node [style=filled,color=white];
> label = "cluster1"

See example below. There seems to be some issue (perhaps a bug
in PyGraphviz) in setting default attributes for subgraphs.
So you may not be able to get exacdtly what you want or expect
there. I'll see if I can figure out what the problem is.

import pygraphviz as p

G = p.AGraph(directed=True)


G.add_node(1)
G.add_node(2)
G.add_node(3)
G.add_node(4)

G.add_edge(1,3)

G.node_attr['color']='red'
G.node_attr['style']='filled'
G.graph_attr['label']='a graph'

G1 = G.subgraph(nbunch=[1,2],
name="cluster1",
style='filled',
color='lightgrey',
label='cluster 1 label')

# this doesn't work correctly
#G1.node_attr['style']='filled'
#G1.node_attr['color']='blue'

attributes={}
attributes.update(style='filled',
color='yellow',
label='cluster 2 label')

G2 = G.subgraph(nbunch=[3,4],name="cluster2",**attributes)

Aric

Guilhem Faure

unread,
May 6, 2009, 9:27:41 AM5/6/09
to pygraphvi...@googlegroups.com
Thank you so much Aric. It works very well now thank to your help !

Guilhem
Reply all
Reply to author
Forward
0 new messages