Adding subgraphs to graphs and writing them to dot files

3,448 views
Skip to first unread message

sk

unread,
Sep 1, 2010, 8:55:08 AM9/1/10
to networkx-discuss
Hi,

I am currently trying to create a subgraphs within a networkx graph
such that when I write it to a dot file the nodes are placed into a
subgraph element. Is this possible?

I can generate a subgraph and have this returned to a new graph,
however this does not have any effect on the dot file created. I would
use this method (on an existing graph):
H = subgraph(G, node_set_1)
K= subgraph(G, node_set_2)

As I say this doesn't (probably as expected) have any effect on G.

I suspect I need to somehow add these subgraphs back to the original
graph, but I haven't yet worked this part out.

Many thanks for your help.

Aric Hagberg

unread,
Sep 1, 2010, 9:11:15 AM9/1/10
to networkx...@googlegroups.com

Graphviz has a very specific representation of subgraphs that is
different than NetworkX. With Graphviz subgraphs included in the
main graph data structure.

If you just need to produce a dot file you can use pygraphviz directly

import pygraphviz as pgv

G=pgv.AGraph()

G.add_edge(1,2)
G.add_edge(2,3)
H=G.subgraph([2,3],name='test') # H is not needed but does contain the
graph with edge 2-3

G.write()

$ python tsg.py
strict graph {
subgraph test {
2 -- 3;
}
1 -- 2;
}

Aric

sk

unread,
Sep 1, 2010, 9:22:51 AM9/1/10
to networkx-discuss
Thanks for such a speedy reply Aric,

Unfortunately I am forced to run my software under windows and hence
pygraphviz is not an option - it will not compile and intall (and
work) for me.

Thank you all the same - it has saved me a lot of head scratching!

regards
Stephen

On Sep 1, 2:11 pm, Aric Hagberg <ahagb...@gmail.com> wrote:
> Aric- Hide quoted text -
>
> - Show quoted text -

Aric Hagberg

unread,
Sep 1, 2010, 9:35:26 AM9/1/10
to networkx...@googlegroups.com
On Wed, Sep 1, 2010 at 7:22 AM, sk <6b65...@gmail.com> wrote:
> Thanks for such a speedy reply Aric,
>
> Unfortunately I am forced to run my software under windows and hence
> pygraphviz is not an option - it will not compile and intall (and
> work) for me.
>

In principle PyGraphviz should work on Windows and some people have had success.
But so far no Windows developers have volunteered to package PyGrapyhviz.

You might try Pydot (which is pure Python).

e.g. (mixed with networkx)

import networkx as nx
import pydot

G=nx.Graph()
G.add_edge(1,2)
G.add_edge(2,3)

P=nx.to_pydot(G) # switch to a Pydot representation
S=pydot.Subgraph('test')
e=pydot.Edge(pydot.Node(2),pydot.Node(3))
S.add_edge(e)
P.add_subgraph(S)
print P.to_string()

The output is different than with PyGraphviz:
$ python tsg2.py
strict graph G {
1;
2;
3;
1 -- 2;
2 -- 3;


subgraph test {
2 -- 3;
}

}

Aric

sk

unread,
Sep 2, 2010, 4:59:39 AM9/2/10
to networkx-discuss
Thank you once again Aric,

This has solved the issue - it is a tiny bit annoying to maintain two
graphs (one for display and one for, say, navigating a shortest path),
but it is a minor inconvenience. It would be great to get pygraphviz
working under windows, that's for sure.

If anyone else uses pydot under windows I did have to remove the
attempt to get the location of graphvis from the windows registry
before I was able to use dot/neato etc as it caused an unhandled
exception.

Regards
Stephen

On Sep 1, 2:35 pm, Aric Hagberg <ahagb...@gmail.com> wrote:

sk

unread,
Sep 3, 2010, 5:54:28 AM9/3/10
to networkx-discuss
Hi,

I have now managed to get a graph that looks as i want it to when
rendered by dot. Is it posible to read in the positions from the dot
file or similar in order to get the x,y coordinates of the nodes?

I have tried to use both the from_pydot and from_dot functions but i
don't beleive that gives me access to the pos data as rendering the
resulting graph gives a very strange layout. naturally i could parse
the dot file manually, but that seems like it would not be the best
option.

essentially once i have a list of node locations i will use pysvg to
render the final image i am after (i need greater control over the
output than i am capable of acheiving with dot alone).

Many thanks

Stephen

franck kalala

unread,
Sep 3, 2010, 6:53:26 AM9/3/10
to networkx...@googlegroups.com
Hey all,

I am trying to plot the node-node-distance-distribution for a given network (not
the histogram) after several runs or iterations. I have tried some thing like
this


import networkx
import pylab
iterations=20
average=0
for i in range(iterations):
G=networkx.gnm_random_graph(1000,4000)
for n in G:
p=networkx.single_source_shortest_path_length(G,n).values()
lengths.extend(p)
n,bins=pylab.histogram(lengths,bins=max(lengths))
average=average+n/iterations
pylab.plot(bins,average/sum(average))

But am having a problem, bins and average have not the same size. how can make
them to have the same size? is there any routine that can plot this in networkx?

Franck



Moritz Beber

unread,
Sep 3, 2010, 8:54:59 AM9/3/10
to networkx...@googlegroups.com
Hello Stephen,

you could use one of the networkx layout algorithms:

http://networkx.lanl.gov/reference/drawing.html#module-networkx.drawing.layout

They usually return the positions as a dictionary which you can then
pass to one of the drawing mechanisms with keyword pos=dict.

Best,
Moritz

Aric Hagberg

unread,
Sep 3, 2010, 9:02:33 AM9/3/10
to networkx...@googlegroups.com
On Fri, Sep 3, 2010 at 3:54 AM, sk <6b65...@gmail.com> wrote:
> Hi,
>
> I have now managed to get a graph that looks as i want it to when
> rendered by dot. Is it posible to read in the positions from the dot
> file or similar in order to get the x,y coordinates of the nodes?
>

Yes, take a look at the "pydot_layout" function in
https://networkx.lanl.gov/trac/browser/networkx/networkx/drawing/nx_pydot.py
That shows how to use Pydot to produce a layout and read the positions
of the nodes.

Aric

sk

unread,
Sep 3, 2010, 9:25:02 AM9/3/10
to networkx-discuss
Thanks for the suggestion Moritz, unfortunately i need the layout of
the nodes as created using multiple subgraphs in pydot and rendered in
DOT. I havent been able to get the same layout without using the
subgraphs feature which therefore means i can't use netoworx to
acheive this.

As to the use of pydot_layout the problem is that this accepts a
networkx graph only and my graph with correctly positioned nodes is in
pydot 'format'. If i convert my pydot graph to networkx it seems to
loose a lot of the important features (subgraphs). Therefore is there
a way to get the pos data from the written dot file (as created with
pydot's write_dot() function or directly from the pydot graph object?
or should networkx be able to cope with a graph, with subgraphs, layed
out by pydot?

I would be happy to post the dot file if it would help?

regards

Stephen

On Sep 3, 2:02 pm, Aric Hagberg <ahagb...@gmail.com> wrote:
> On Fri, Sep 3, 2010 at 3:54 AM, sk <6b656...@gmail.com> wrote:
> > Hi,
>
> > I have now managed to get a graph that looks as i want it to when
> > rendered by dot. Is it posible to read in the positions from the dot
> > file or similar in order to get the x,y coordinates of the nodes?
>
> Yes, take a look at the "pydot_layout" function inhttps://networkx.lanl.gov/trac/browser/networkx/networkx/drawing/nx_p...

Aric Hagberg

unread,
Sep 3, 2010, 9:30:50 AM9/3/10
to networkx...@googlegroups.com
On Fri, Sep 3, 2010 at 7:25 AM, sk <6b65...@gmail.com> wrote:
> Thanks for the suggestion Moritz, unfortunately i need the layout of
> the nodes as created using multiple subgraphs in pydot and rendered in
> DOT. I havent been able to get the same layout without using the
> subgraphs feature which therefore means i can't use netoworx to
> acheive this.
>
> As to the use of pydot_layout the problem is that this accepts a
> networkx graph only and my graph with correctly positioned nodes is in
> pydot 'format'. If i convert my pydot graph to networkx it seems to
> loose a lot of the important features (subgraphs). Therefore is there
> a way to get the pos data from the written dot file (as created with
> pydot's write_dot() function or directly from the pydot graph object?
> or should networkx be able to cope with a graph, with subgraphs, layed
> out by pydot?

Yes, pydot_layout won't work for you directly but you can use the code
contained within to do what you want. It's not pretty but it will
work.

e.g. you have a pydot graph object called P that you like.
now (from pydot_layout():

D=P.create_dot(prog=prog)

if D=="": # no data returned
print("Graphviz layout with %s failed"%(prog))
print()
print("To debug what happened try:")
print("P=pydot_from_networkx(G)")
print("P.write_dot(\"file.dot\")")
print("And then run %s on file.dot"%(prog))
return

Q=pydot.graph_from_dot_data(D)

node_pos={}
for n in G.nodes():
node=Q.get_node(pydot.Node(n).get_name())
pos=node.get_pos()[1:-1] # strip leading and trailing double quotes
if pos != None:
xx,yy=pos.split(",")
node_pos[n]=(float(xx),float(yy))

Aric

sk

unread,
Sep 3, 2010, 10:56:15 AM9/3/10
to networkx-discuss
Thanks again Aric, it seems that this will help me do what I need to.

Thank you very much once, your patience and knowledge is most
appreciated.

On Sep 3, 2:30 pm, Aric Hagberg <ahagb...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages