Reducing the size (as in area) of the graph generated by graphviz
Asked Answered
F

1

65

Does anyone have any general tips for reducing the size of a graph generated by graphviz (size as in area, not as in file size).

I have a fairly large graph (700 nodes). I set a smaller font size for each node, but it seems to only reduce the font size and not the actual node size. Are there any attributes to reduce the overall amount of blank space in the graph also? Thanks!

Flange answered 6/8, 2010 at 23:17 Comment(2)
This #1287313 answer is about the most elegant and least work to implement.Entero
@Entero no--the answer you link to is in summary "to make the graph smaller, "zoom out"--fine, that works for probably rendering any digital image. GraphViz is a graph layout engine, therefore, a useful answer to questions about reducing graph size are most usefully directed to the layout of the graph itself, because that's the essence of what GraphViz does, which is layout graphs. What's more, teh "zoom out" solution doesn't address eg, large regions of white-space or sparsity, but techniques for better layout in fact do.V1
V
108

In my experience using graphviz to render graphs of that size (~ 700 nodes), minimal trial-and-error adjustment to this combination of attribute settings--some structural, some purely aesthetic--for all three objects (graph, nodes, and edges) should do what you want:

reduce the minimum separation between nodes, via 'nodesep'; e.g., nodes[nodesep=0.75]; this will make your graph being "too compact." (nodesep and ranksep probably affect how dot draws a graph more than any other adjustable parameter)

reduce the minimum distance between nodes of different ranks, e.g, nodes[ranksep=0.75]; 'ranksep' sets the minimum distance between nodes of different ranks--this will affect your graph layout significantly if your graph is comprised of many ranks

increase the edge weights, eg, edge[weight=1.2]; this will make the edges shorter, in turn making the entire graph more compact

remove node borders and node fill, e.g., nodes[color=none; shape=plaintext], especially for oval-shaped nodes, a substantial fraction of the total node space is 'unused' (ie, not used to display the node label); each node's footprint is now reduced to just its text

explicitly set the font size for the nodes (the node borders are enlarged so that they surround the node text, which means that the font size and amount of text for a given node has a significant effect on its size); [fontsize=11] should be large enough to be legible yet also reduce the 'cluttered' appearance (the default size is 14)

use different colors for nodes and edges--this will make your graph easier to read; e.g., set the node 'text' fontcolor to blue and the edge fontcolor to "grey" to help the eye distinguish the two sets of graph structures. This will make a bigger difference than you might think.

explicitly set total graph size, eg, graph[size="7.75,10.25"] (ensures that your graph fits on an 8.5 x 11 page and that it occupies the entire space)

V1 answered 6/8, 2010 at 23:50 Comment(6)
Thanks for the detailed response. The only problem I have is that setting the font size doesn't seem to reduce the overall node size for it. It seems like the only thing that gets smaller is the font.Flange
no problem. Regarding 'fontsize', if you removed the node borders and node fill first, so that the representation of each node is just the node text, then reducing the text size is exactly what you want, because it reduces the 'apparent' node size. (Granted, removing the node borders/fill doesn't always improve overall 'resolution'--just depends on your graph, but you'll know as soon as you try it).V1
The suggestions above are great and provide much more power than what I do... cat [something.dot] | dot-Gdpi=64 -Tpng:cairo:cairo > [outfile.png] which just scales the graph...Entero
Changing the node edge to grey really made a difference especially when you have to pore over huge messy graphs. Thanks!Gastrocnemius
nodes[nodesep=0.75] doesn't work anymore, it has to be put directly into the graph. Same for ranksepMonatomic
@GeorgesDupéron: Can you give an example? Alternatively, and maybe even better, can you make an edit to the answer?Knighton

© 2022 - 2024 — McMap. All rights reserved.