Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: Re: Size of PNG graphs: xsize, ysize don't work

From   Friedrich Huebler <>
Subject   st: Re: Size of PNG graphs: xsize, ysize don't work
Date   Mon, 22 Aug 2005 22:02:18 -0700 (PDT)

Someone wrote to me and asked if I found a solution to a problem that
I described in a posting to Statalist in March. Yes, I did and I
apologize for not having shared the solution with the list sooner.

I was looking for a way to specify the size of PNG graphs that are
exported from Stata. Stata tech support suggested that this can be
done by exporting the graph as a PostScript file and converting it to
PNG format with a third-party utility like Ghostscript
( Ghostscript can be called from a do-file via a
shell command to automate the creation of PNG files. The example
below creates a PNG file with the dimensions 500 by 400 pixels.

#delimit ;
sysuse auto;

* Set options for PostScript export;
graph set ps pagesize custom;
graph set ps pagewidth 5;
graph set ps pageheight 4;
graph set ps tmargin 0;
graph set ps lmargin 0;
graph set ps logo off;
graph set ps fontface Helvetica;

* Draw graph and export to PostScript file;
scatter mpg weight, xsize(5) ysize(4);
graph export "";

* Convert PostScript file to PNG format;
shell "C:\gs\gs8.51\bin\gswin32c.exe"
  -r100 -g500x400 -sDEVICE=png16m
  -dGraphicsAlphaBits=4 -dTextAlphaBits=4
  -sOutputFile="scatter.png" "";

A detailed explanation of the process an be found at these sites
(both contain the same text):

I work with Stata 8.2 and don't know if this is still an issue with
Stata 9.

Friedrich Huebler

--- Friedrich Huebler <> wrote:
> I would like to export graphs to PNG format with a specific size,
> for example 500 by 375 pixels. Apparently the only way to get this
> size is through trial and error by repeatedly resizing the graph
> window, exporting the graph, and checking its size in an image
> viewer.
> The xsize(#) and ysize(#) options don't have an effect, at least
> when the ratio x/y doesn't change. David Harrison mentioned this in
> a post to Statalist last month
> ( You
> can try this yourself:
> . sysuse auto
> . scatter mpg weight, xsize(5) ysize(4)
> . scatter mpg weight, xsize(2.5) ysize(2)
> . scatter mpg weight, xsize(2.5) ysize(1)
> . scatter mpg weight, xsize(5) ysize(2)
> The first two -scatter- commands draw graphs of exactly the same
> size, as do the last two -scatter- commands. The graph changes from
> the second to the third -scatter- command because the ratio x/y is
> different.
> Has anyone found a way to create PNG graphs of a specific size that
> doesn't require manual resizing of the graph window? Does Statacorp
> plan to add an option to specify graph size in pixels?
> Thank you,
> Friedrich Huebler

Start your day with Yahoo! - make it your home page 
*   For searches and help try:

© Copyright 1996–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index