Drawing a Bipartite Graph

1,250 views
Skip to first unread message

AliReza Khoshgoftar Monfared

unread,
May 5, 2011, 4:04:14 PM5/5/11
to pygraphvi...@googlegroups.com
Hi,
I was wondering if there is an easy way to draw a bipartite graph using pygraphviz having two sets of the nodes placed separately with edges running between them.
I have seen that there is a 'pos' attribute that can be used to set the placement of each node, but I was wondering if there is an easier solution so that I can specify a binary type for each node in the bipartite graph and have a better visualization of the "bi-partite" property of the graph...

Aric Hagberg

unread,
May 5, 2011, 4:26:30 PM5/5/11
to pygraphvi...@googlegroups.com

If you can control the order of the nodes in the edgesas you add them,
then the dot file order reflects the bipartite structure. In that
case the drawings come out how you want. The following produces two
rows of nodes with edges between:

In [1]: import pygraphviz as pgv

In [2]: G=pgv.AGraph()

In [3]: G.add_edge('a',1)

In [4]: G.add_edge('a',2)

In [5]: G.add_edge('b',2)

In [6]: G.add_edge('c',2)

In [7]: G.add_edge('c',3)

In [8]: G
Out[8]:
strict graph {
a -- 1;
a -- 2;
b -- 2;
c -- 2;
c -- 3;
}

In [9]: G.layout(prog='dot')

In [10]: G.draw('test.png')


Aric

AliReza Khoshgoftar Monfared

unread,
May 5, 2011, 4:44:12 PM5/5/11
to pygraphvi...@googlegroups.com
Thanks,
I was actually doing everything like your sample code, except that I was not setting the layout prog to 'dot'
It's fixed now, but another problem is that since my graph has about 900 edges and a comparable number of nodes the plot looks like that there is a thick black line between the two types of nodes 
Is there a way to increase the quality/resolution of the output, and/or make it of a bigger size so that the actual edges can be seen after zooming?

Aric Hagberg

unread,
May 5, 2011, 4:55:10 PM5/5/11
to pygraphvi...@googlegroups.com

Yes, you can set all of the dot file attributes
http://www.graphviz.org/content/attrs
You can probably change some of the graph attributes to adjust the size, etc.
Use the "graph_attr" data dictionary like this:


In [1]: import pygraphviz as pgv

In [2]: G=pgv.AGraph()

In [3]: G.graph_attr['rankdir']='LR'

In [4]: G.graph_attr['splines']='true'

In [5]: G
Out[5]:
strict graph {
graph [rankdir=LR,
splines=true
];
}


Aric

Reply all
Reply to author
Forward
0 new messages