Placing nodes vertically in Graphviz using pydot
Asked Answered
L

3

13

I am using Graphviz in Python via pydot. The diagram I am making has many clusters of directed graphs. pydot is putting them next to each other horizontally resulting in an image that is very wide. How can I tell it to output images of a maximum width so that I can scroll vertically instead?

Lattimore answered 7/2, 2010 at 1:6 Comment(0)
Z
13

There are several things you can do.

  1. You can set the maximum size of your graph, using 'size' (e.g., size = "4, 8" (inches)). This fixes the size of your final layout. Unlike most other node,edge, and graph parameters in the dot language, 'size' has no default. Also, the default orientation is 'portrait', which i believe is what you want (for a graph that is taller vs. wider), but you might want to set this parameter explicitly in case it was set to 'landscape' earlier.

  2. 'Size' can be used with the 'ratio' parameter (the layout aspect ratio) to manipulate the configuration. 'Ratio' takes a float (e.g., ratio = "2.0") or 'auto' or 'fill'. (The latter tells graphviz to fill use the entire graph region alloted by 'size'.

  3. The parameters that have the greatest effect on graph configuration are 'nodesep' and 'ranksep'. These are the minimum horizontal distance between adjacent nodes of equal 'rank', and the minimum vertical distance between adjacent ranks of nodes. The default values are 0.25 and 0.75 inches, respectively. To get the configuration you want, you will want to simultaneously increase nodesep and decrease ranksep. Gradual iteration should allow you to quickly converge on a set of values for these two parameters that gives you the configuration you want.

Ziska answered 7/2, 2010 at 3:59 Comment(0)
E
13

Initialize your graph like this:

graph = pydot.Dot(graph_type='digraph', rankdir='LR')

This will set the graph direction from left to right. In general, use the graphviz documentation to find the right attribute in order to achieve what you want.

Escalator answered 1/5, 2013 at 3:26 Comment(2)
+1 I was carrying out an extensive search on pydot documentation and until I saw your answer I was not aware that the graphviz documentation serves as pydot documentation. I tested a couple of features and parameters in the link yuo gave and all is clear now.Cavy
Glad to help. I remember stumbling over this issue multiple times myself until I finally figured this out - this setting should clearly be the default IMO, since it's much more useful with text printed horizontally in the nodes.Intimidate
C
7

I'm not sure if you're able to do this with your data, but if you change the order that the nodes are inserted into the graph it can really affect the generated graph. If you don't want to supply any ordering information to Graphviz and want Graphviz to attempt solving optimal placement of nodes to minimize contention, use Graphviz's neato instead. It uses a spring model to figure out where nodes should be placed.

It looks like you should be able to use neato inside pydot like:

my_graph.write('my_graph.png', prog='neato', format='png')

See pydot's documenation here.

Caddis answered 7/2, 2010 at 1:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.