Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get nodes coordinates in graph rendered with graphviz

I'm implementing a stepwise graph transformation algorithm where nodes are removed or added in each step and want to keep a trace of all the intermediate graphs as images files where a node keeps rendered at the same position until removed. This will help make a final animation.

I thought about getting the nodes positions from the initial step as computed by the layout engine and then pass them as a node attribute for next steps.

I'm using the graphviz library, but I could not find any way to get the nodes coordinates (pos attribute) in the rendered graphs. Here is a code excerpt.

    from graphviz import Digraph
    dot = Digraph()
    dot.node('x', label='hello')
    dot.node('y', label='world')
    dot.edge('x', 'y')
    dot.render(filename='hello.gv', view=True, cleanup=False)

I also inspected the dot object, but found nothing. Am I missing some thing? I could not conform whether positions are exported via the API or no. In this case, which different library can help?

like image 252
Brahim Gaabab Avatar asked Jul 31 '19 11:07

Brahim Gaabab


People also ask

How do you order nodes in Graphviz?

If ordering="out" , then the outedges of a node, that is, edges with the node as its tail node, must appear left-to-right in the same order in which they are defined in the input. If ordering="in" , then the inedges of a node must appear left-to-right in the same order in which they are defined in the input.

How do I use DOT in Graphviz?

How do I use graphviz to convert this into an image? For windows: dl the msi and install; Find gvedit.exe in your programs list; Open . dot file in question; Click running person on toolbar; Go to graph -> settings ; change Output file type to file type of your liking and press ok..

What is rank in Graphviz?

Ranks and Subgraphs To work out the layout, Graphviz uses a system it calls "ranks". Each node is assigned a higher rank than the highest ranked node that point to it. If your rank direction is set to left to right ( rankdir=LR ), then nodes with a higher rank are placed further to the right.

What is Graphviz Neato?

NEATO is a program that makes layouts of undirected graphs following the. filter model of DOT. Its layout heuristic creates virtual physical models and runs an iterative solver to find low energy configurations.


1 Answers

First of all, you need to pipe and decode your Digraph object to JSON format. Afterwards, you can convert the JSON string to a JSON object and parse it:

# import JSON parser
import json

# decode the Digraph to JSON format
json_string = dot.pipe('json').decode()

# parse the resulting json_string
json_dict = json.loads(json_string)

# now, you have a JSON dictionary that has two relevant keys:
# 'objects' for nodes and 'edges' for, well, edges.
# you can iterate over the nodes and get the (x,y) position for each node as follows:
for obj in json_dict['objects']:
    print(obj['name'], '\t', obj['pos'])

I had to try each format in graphviz.FORMATS to find out the most suitable one.

I know it might be too late, but just in case you or anybody else needs the answer.

like image 188
Rashedkoutayni Avatar answered Sep 29 '22 23:09

Rashedkoutayni