Radial Layout using Pydot

710 views
Skip to first unread message

ruchit

unread,
Oct 22, 2009, 1:16:30 AM10/22/09
to networkx-discuss
Hello,

I am trying to using the radial layout of GraphViz (prog='twopi')
through pydot in Networkx.

Initially I tried using Pygraphviz but could not link it with my
Python. I guess there is some compiler issue. Anyways i gave up trying
and switched to using Pydot.

I installed GraphViz 2.24 and Pydot 1.0.2 with Python 2.5. Now when i
use <pos=nx.pydot_layout(K,prog='twopi',root=0)> or only
<pos=nx.pydot_layout(K)> it points to pydot.py and gives the following
error:

<edge_attr.append( attr + '=' + quote_if_necessary(value) )
TypeError: cannot concatenate 'str' and 'int' objects>

Just tried my luck by editing the file to

<edge_attr.append( str(attr) + '=' + str(quote_if_necessary(value)) )>

Then it points to nx_pydot.py and gives the following error:

<pos=node.get_pos()[1:-1] # strip leading and trailing double quotes
AttributeError: 'list' object has no attribute 'get_pos'>

Can anyone help me get the radial layout working through Pydot.

Thanks,

Ruchit

Aric Hagberg

unread,
Oct 22, 2009, 7:53:29 AM10/22/09
to networkx...@googlegroups.com
On Wed, Oct 21, 2009 at 11:16 PM, ruchit <watzi...@gmail.com> wrote:
> I installed GraphViz 2.24 and Pydot 1.0.2 with Python 2.5. Now when i
> use <pos=nx.pydot_layout(K,prog='twopi',root=0)> or only
> <pos=nx.pydot_layout(K)> it points to pydot.py and gives the following
> error:
>
> <edge_attr.append( attr + '=' + quote_if_necessary(value) )
> TypeError: cannot concatenate 'str' and 'int' objects>
>
> Just tried my luck by editing the file to
>
> <edge_attr.append( str(attr) + '=' + str(quote_if_necessary(value)) )>
>
> Then it points to nx_pydot.py and gives the following error:
>
> <pos=node.get_pos()[1:-1] # strip leading and trailing double quotes
> AttributeError: 'list' object has no attribute 'get_pos'>
>
> Can anyone help me get the radial layout working through Pydot.

What is in your graph K?

It works for me with this configuration:

In [1]: import networkx

In [2]: G=networkx.path_graph(4)

In [3]: networkx.pydot_layout(G,prog='twopi',root=0)
Out[3]: {0: (28.0, 19.0), 1: (28.0, 91.0), 2: (28.0, 163.0), 3: (28.0, 235.0)}

In [4]: networkx.__version__
Out[4]: '1.0.dev1487'

In [5]: import pydot

In [6]: pydot.__version__
Out[6]: '1.0.2'

In [7]: !dot -V
dot - Graphviz version 2.20.2 (Wed Sep 16 11:12:21 UTC 2009)

Aric

ruchit

unread,
Oct 22, 2009, 3:23:52 PM10/22/09
to networkx-discuss
My code is the following:

ln [1]: import networkx

ln [2]: import pydot

ln [3]: from networkx import pydot_layout

ln [4]: K=nx.MultiDiGraph()

ln [5-9]: # Add edges from another graph based on certain conditions
for (u,v,d) in H.edges(data=True):
if float(d['prob']) > 0.98:
K.add_node(u+str(d['path']))
K.add_node(v+str(d['path']))
K.add_edge(u+str(d['path']),v+str(d['path']),prob=d
['prob'])

ln [10]: pos=nx.pydot_layout(K)

Error: edge_attr.append( attr + '=' + quote_if_necessary(value) )
TypeError: cannot concatenate 'str' and 'int' objects

alternate ln[10]: pos=nx.spring_layout(K) # This works fine.

As of now I just want to make sure that i can get the pydot layouts.
Once I am sure it is working I will use the prg twopi and assign the
root.

I would be gald and thankful if you could point out my error and help
me correct it. I have a suspicion that it does require nodes to be
integers. Correct me if I am wrong.

Thanks and regards,

Ruchit

On Oct 22, 7:53 am, Aric Hagberg <ahagb...@gmail.com> wrote:

ruchit

unread,
Oct 22, 2009, 3:44:07 PM10/22/09
to networkx-discuss
There was a typo in the previous post. The actual ln[1] is as follows:

Correction: The ln[1]: import networkx as nx

Rest of the lines remain the same. I would be glad and thankful if
someone could help me rectify the error.

Ruchit

ruchit

unread,
Oct 22, 2009, 4:00:02 PM10/22/09
to networkx-discuss
I made a small change. Before adding the edge i defined the position.
So the updated code is:

ln [1]: import networkx as nx
ln [2]: import pydot
ln [3]: from networkx import pydot_layout
ln [4]: K=nx.MultiDiGraph()
ln [5-8]: # Add nodes from another graph based on certain conditions
for (u,v,d) in H.edges(data=True):
if float(d['prob']) > 0.98:
K.add_node(u+str(d['path']))
K.add_node(v+str(d['path']))

ln [10]: pos=nx.pydot_layout(K)

ln [11-13]: # Add nodes from another graph based on certain
conditions
for (u,v,d) in H.edges(data=True):
if float(d['prob']) > 0.98:
K.add_edge(u+str(d['path']),v+str(d['path']),prob=d
['prob'])

Ruchit

ruchit

unread,
Oct 22, 2009, 4:03:22 PM10/22/09
to networkx-discuss
And the error it is showing me now is:

In the file: nx_pydot.py
pos=node.get_pos()[1:-1] # strip leading and trailing double quotes
AttributeError: 'list' object has no attribute 'get_pos'

Ruchit

ruchit

unread,
Oct 22, 2009, 4:04:09 PM10/22/09
to networkx-discuss
And the error it is showing me now is:

In the file: nx_pydot.py
pos=node.get_pos()[1:-1] # strip leading and trailing double quotes
AttributeError: 'list' object has no attribute 'get_pos'

Ruchit

On Oct 22, 4:00 pm, ruchit <watzinn...@gmail.com> wrote:

Aric Hagberg

unread,
Oct 22, 2009, 4:15:49 PM10/22/09
to networkx...@googlegroups.com
On Thu, Oct 22, 2009 at 1:44 PM, ruchit <watzi...@gmail.com> wrote:
>
> There was a typo in the previous post. The actual ln[1] is as follows:
>
> Correction: The ln[1]:  import networkx as nx
>
> Rest of the lines remain the same. I would be glad and thankful if
> someone could help me rectify the error.
>
I think it's a bug that all data sent from NetworkX to Pydot wasn't
converted to strings. This came up in your case and not mine because
you were using MultiGraph. The nodes and edges were converted to
strings but not the
"key" for MultiGraphs.

I checked in what I hope is a fix:
https://networkx.lanl.gov/trac/changeset/1490

Aric

ruchit

unread,
Oct 22, 2009, 5:30:08 PM10/22/09
to networkx-discuss
Thanks Aric.

That seems to have solved the first problem. However it didn't address
the second problem. It still gives me the following error:

In the file: nx_pydot.py
pos=node.get_pos()[1:-1] # strip leading and trailing double quotes
AttributeError: 'list' object has no attribute 'get_pos'

Am i missing something?

Ruchit

On Oct 22, 4:15 pm, Aric Hagberg <ahagb...@gmail.com> wrote:

Aric Hagberg

unread,
Oct 22, 2009, 10:36:22 PM10/22/09
to networkx...@googlegroups.com
On Thu, Oct 22, 2009 at 3:30 PM, ruchit <watzi...@gmail.com> wrote:
>
> Thanks Aric.
>
> That seems to have solved the first problem. However it didn't address
> the second problem. It still gives me the following error:
>
> In the file: nx_pydot.py
> pos=node.get_pos()[1:-1] # strip leading and trailing double quotes
> AttributeError: 'list' object has no attribute 'get_pos'
>
> Am i missing something?
>

Might be a bug - here are some things to try:

In [1]: import networkx

In [2]: import pydot

In [3]: G=networkx.MultiGraph()

In [4]: G.add_edge(1,2,foo='bar')

In [5]: G.add_edge(1,2,foo='baz')

In [6]: P=networkx.to_pydot(G) # convert to pydot object

In [7]: D=P.create_dot() # dot-file as a string (with positions)

In [8]: D
Out[8]: 'graph G {\n\tnode [label="\\N"];\n\tgraph
[bb="0,0,54,112"];\n\t1 [pos="27,93", width="0.75",
height="0.51389"];\n\t2 [pos="27,19", width="0.75",
height="0.51389"];\n\t1 -- 2 [key=0, foo=bar, pos="21.084,74.708
19.965,63.238 19.972,48.512 21.105,37.082"];\n\t1 -- 2 [key=1,
foo=baz, pos="32.916,74.708 34.035,63.238 34.028,48.512
32.895,37.082"];\n}\n'

In [9]: Q=pydot.graph_from_dot_data(D) # convert back to pydot object

In [11]: Q.get_node('1').get_pos() # position of node 1
Out[11]: '"27,93"'

In [12]: Q.get_node('1').get_pos()[1:-1] # quotes stripped
Out[12]: '27,93'


Aric

Reply all
Reply to author
Forward
0 new messages