Gnuplot and Stata

June 1, 2009 at 2:30 pm 3 comments

| Gabriel |

Stata has had pretty good graphs for a few years, but it can’t do everything, for instance contours. This means that if you’re interested in doing these graphs you either need to export to a GUI spreadsheet or charting package (preferably Excel, which is very versatile, despite the hideous default settings of yellow and pink lines on gray background) or you need to pipe things to a program that can batch it, like R or Gnuplot. I think batching is generally preferable to GUI for the simple reason that we usually don’t do things once but repeat it with subtle variations about a million times. As such I’ve been experimenting with Gnuplot though I readily admit that the same things would work (and perhaps work better) with R.

The first step is getting it. Since the whole point is to have Stata pipe to it it should be running on the same OS rather than on a separate computer or in a VM (unless you’re really comfortable with ssh and that sort of thing). If you’re using Linux, gnuplot might already be installed and if not you can probably get it with your package manager either directly or by installing a program (eg, Octave) that has it as a dependency. Likewise you can download the source code and binaries, including Windows (“Win32”).

Unfortunately it’s a bit harder to get for the Mac. As with a lot of Unix software the standard solution is to use Fink but as mentioned before I can’t get Fink to work consistently. I have found a binary of Gnuplot, but since it’s not obvious where to look I’ll share it:

  1. Download Octave (basically a clone of Matlab)
  2. Open the disk image for Octave but you don’t need to install it if you don’t want it.
  3. In the dmg open “/extras” and open the Gnuplot disk image.
  4. Install Gnuplot.

Furthermore, I recommend that you use a text editor to open (or create) the file “~/.bashrc” and add the line:

alias gnuplot="exec '/Applications/'"

The next step is to write a do-file that pipes your data and command from Stata to Gnuplot. The easy part is to outsheet the relevant subset of your data to tab-delimited text. Next you need to write the Gnuplot command. You could simply write it all into a single shell command, but I think it’s easier to write it to a text file then use the shell command to have Gnuplot execute the text file. (Why is it easier? Because it makes it much easier to make specific parts of the Gnuplot command contingent on options you specify from within Stata). This requires use of Stata’s “file” command. One trick is that because both Gnuplot and Stata read quotes you have to escape the embedded quotes to get Stata to treat them as literal and therefore pass them on to Gnuplot. A simple example of Stata’s use of escaping quotes is here:

disp `"Then Beavis said "Fire!""'

Whereas Gnuplot has a minimalist graph command (just “plot,” “splot,” or “replot” followed by the filename) Stata puts almost everything in the graph command itself. Gnuplot handles all these options by a series of “set” commands that you specify before running the graph. Because Gnuplot recognizes the semicolon delimiter you can put several commands on one line. So you “set terminal” (which in batch mode should be a file format like postscript but in interactive mode might be a display protocol like X11), set the title and axes titles, set the saving path, set the type of graph, etc. If this is confusing just think of it as changing the preferences, then doing a graph based on the preferences rather than (as Stata does) specifying everything on an ad hoc basis.

Below I’ve written a Stata program that will send data to Gnuplot, write a Gnuplot command, and execute that Gnuplot command. Because I’m too lazy to make it flexible right now the command is only good for doing contour plots (using the pm3d command which produces a color gradient rather the topographic lines of the standard contour command). However it’s entirely feasible to write a generic wrapper to pipe Stata to Gnuplot. The easy-to-program but hard-to-use approach would just have the user write the Gnuplot command from within Stata but facilitate piping the data and command. The hard-to-program but easy-to-use approach would be to write a wrapper that translates Stata syntax to Gnuplot syntax. Anyway, here’s my much more limited command for making a contour plot, followed by a sample of the output.

[Update 12/14/09, now uses the “tempfile” local to avoid leaving behind a bunch of command and data text files]

capture program drop gnuplotpm3d
program define gnuplotpm3d
syntax varlist(min=3 max=3 numeric) [ , title(string asis) xlabel(string asis) ylabel(string asis) using(string asis)]

if "`xlabel'"=="" {
local xlabel="`x'"
if "`ylabel'"=="" {
local ylabel="`y'"
tempfile gnuplotpm3cmd
tempfile data

keep `varlist'
order `varlist'
disp "`using'"
outsheet using `data'.txt, replace

local gnuplot="exec '/Applications/'" /*this line is necessary if Stata can't find gnuplot, non-mac users may need to edit it*/

file open gpcmd using `gnuplotpm3cmd', write text
file write gpcmd "cd '`c(pwd)'' ; " _n
file write gpcmd "set terminal postscript color ; set output '`using'.eps' ; set palette color positive ; " _n
file write gpcmd "set auto ; set parametric ; set dgrid3d ; " _n
file write gpcmd `"set title "`title'" ; set ylabel "`ylabel'"; set xlabel "`xlabel'"; "' _n
file write gpcmd "unset contour; unset surface; " _n
file write gpcmd "set view map; set pm3d; set pm3d interpolate 10,10; " _n
file write gpcmd `"splot "`data'.txt"; "' _n
file close gpcmd

shell `gnuplot' `gnuplotpm3cmd'

Entry filed under: Uncategorized. Tags: , , .

Contour graphs in Stata Anonymous pdf (updated)


  • 1. Mike3550  |  June 4, 2009 at 6:20 pm

    I meant to comment on this post earlier, but haven’t had a chance to as I have been trying to figure out my dissertation revisions. Being able to draw contour graphs is so helpful, thank you! I have been trying to figure out how to do something similar and, along with your previous post on crude ones in Stata, this is going to really help. After I get my dissertation deposited, I definitely want to try these out.

  • 2. gabrielrossman  |  June 4, 2009 at 10:17 pm

    glad it’s useful/fun. btw, i updated the axes label bit at the beginning of the code, i had “==” when i should have had “~=”.

    good luck with your dissertation filing (and presumably orals).

  • 3. Regression to the mean « Code and Culture  |  August 6, 2009 at 1:27 pm

    […] use some of my other code to graph the simulation as a contour plot, either crudely but natively or more elegantly with gnuplot. Here’s the code with those two […]

The Culture Geeks

%d bloggers like this: