How to get the coordinates of a graph drawn by specific layout algorithm in graph-tool?
Asked Answered
I

1

6

I want to do the example shown here. It works perfectly well on my machine. The problem is, I can't get the coordinates of the nodes as an array that are stored somewhere in the variable pos. How do I do this?

Thanks in advance for your response!

Ivories answered 2/1, 2013 at 11:42 Comment(2)
What do you mean? Isn't it right there, near the end? pos = arf_layout(g, pos=sfdp_layout(g), max_iter=10000, a=30, d=0.3, weight=w)?Houppelande
No, it isn't. In that line I only assign the values in pos, where the coordinates are stored. I need to extract them from pos.Ivories
H
4

I stuck a import pdb; pdb.Pdb().set_trace() right after the line assigning to pos, and poked at pos. It's a graph_tool.PropertyMap containing vertex attributes:

(Pdb) pos
<PropertyMap object with key type 'Vertex' and value type 'vector<double>', for Graph 0x2efbf90

There doesn't seem to be a direct way to iterate over each vertex from this object, but we can get the graph, and ask the graph for each vertex:

(Pdb) pos.get_graph().vertices()
<graph_tool.libgraph_tool_core.VertexIterator object at 0x2f99950>

This will iterate over each vertex, and we can use it as a key to pos, which is a mapping:

(Pdb) pos[pos.get_graph().vertices().next()]
array([-37.40184702,  25.3717068 ])

Or if you want all of them, you could put this in the sample program:

for vertex in g.vertices():
    print pos[vertex]

Which will print:

array([-37.40184702,  25.3717068 ])
array([-37.5790565 ,  26.77548156])
array([-35.57640651,  24.60125253])
array([-38.90262591,  24.30374   ])
array([-33.72945377,  24.31891936])
array([-32.68247364,  25.85861809])
[...]
Houppelande answered 4/1, 2013 at 14:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.