Search code examples
pythongraphgraphvizpygraphviz

How to set the width and height of the output image in Pygraphviz


I am creating a .png file like this:

import pygraphviz as pgv

G = pgv.AGraph()
G.add_nodes("a")
G.add_edge("b", "c")

G.layout()
G.draw("output.png")

How can I set the size of the output image?


Solution

  • You can set the maximum size of your output image, by setting size, which is an attribute of the graph object. E.g.,

    digraph "my_graph" {
    
         graph[ fontname = "Helvetica-Oblique",
                fontsize = 12,
                label = "some label",
                size = "7.75,10.25" ];
    
         node [ shape = polygon,
                sides = 4 ];
    }
    

    In this example, i've set the graph size to 7.75 x 10.25, which is the size you want, to ensure that your graph fits on an 8.5 x 11 inch sheet and also to ensure that it occupies all of the space on that sheet.