10

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?

Moot
  • 2,195
  • 2
  • 17
  • 14
jbochi
  • 28,816
  • 16
  • 73
  • 90

3 Answers3

6

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.

doug
  • 69,080
  • 24
  • 165
  • 199
  • 1
    size only specifies the _max_ size of the graph...is there a way to specify the _exact_ size og the output? – blue May 15 '13 at 09:36
  • 8
    Note that to change that attribute from within the python code, using `pygraphviz`, you can use the `AGraph`'s `graph_attr.update(attr=value)` method, eg., given your graph is `G`: `G.graph_attr.update(size="2,2")`. More info about available attributes here: http://www.graphviz.org/doc/info/attrs.html – cedbeu Jul 09 '13 at 20:41
  • 2
    How many pixels per inch does it provide? Update: Ah, there's also a DPI attribute you can set: http://www.graphviz.org/doc/info/attrs.html#a:dpi – rakslice Sep 15 '13 at 09:28
1

You can easily set up the resolution with :

G.draw("file.png", args='-Gdpi=200', prog="dot")

Or since it's a graph attribute :

G.graph_attr['dpi'] = '200'

If you wish to avoid concerns regarding resolution, an alternative option is to export the file in a vector format such as .svg.

Arthur R.
  • 323
  • 2
  • 7
0

In addition to setting the size, you might want to enforce a particular aspect ratio too.

To set a ratio of 1:1.4 (width=1, height=1.4, i.e. that of an A4 page, approximately), you can do this (with a maximum dimension of 10 inches):

G.draw('output.png', args='-Gsize=10 -Gratio=1.4', prog='dot')

The format here is:

Graph arguments: -G<arg>; Node arguments: -N<arg>; Edge arguments: -E<arg>.

There's a list of arguments here: https://manpages.debian.org/stretch/graphviz/dot.1.en.html

Hope that helps.

Chris
  • 1