aliquote.org

Compiling Gnuplot on OS X

May 9, 2011

Here are step-by-step instructions for installing Gnuplot with mouse support and all Latex backends on OS X 10.6.

I’ve been using Gnuplot a little bit before discovering R. However, as I recently found myself answering some Gnuplot-related questions on stackoverflow.com and stats.stackexchange.com, I decided to reinstall my old gnuplot which was by far and large broken: no mouse support, no aqua terminal, etc.

So, first of all, I grabbed the latest CVS version of Gnuplot

$ cvs -z3 -d:pserver:anonymous@gnuplot.cvs.sourceforge.net:/cvsroot/gnuplot co \
  -P gnuplot

I wanted aqua support because it has a nice rendering and export to PDF is made easy thanks to the Quartz device. I already have Aquaterm installed on my Airbook, but it is well known that it is only available in 32-bit mode. This means that we need to build Gnuplot for 32-bit (aka, i386) architecture, although I found some webpages showing that we can patch Aquaterm to run in 64-bit mode. Anyway, let’s go for an i386 architecture. I don’t really care because this is what I’m actually running most of the time.

As we also want tikz and lua support (for exporting to Latex), we need liblua. I already have Lua 5.1, but it was built for 64-bit architecture. So, I downloaded it again and recompile it, following the standard instructions. Note that the default is an x86_64 binary whereas I need to have an i386 build too. I used this modified Makefile (replace the default src/Makefile by this one after having removes the suffix). Also, we need to have a dylib library, and not the default liblua.a that is generated by the Makefile. See the end of the Makefile for how to generate the liblua.dylib file (which comes from Building lua dylib on Mac OS X 10.4). In short, in the source directory:

$ make macosx
$ sudo make install
$ make -C src liblua.dylib
$ sudo cp src/liblua.dylib /usr/local/lib/

Ok, now, we have an i386 + x86_64 Lua 5.1. Finally, we just need to copy the lua.pc file to /usr/local/lib/pkgconfig directory.

Then, in the CVS directory for Gnuplot:

$ ./prepare
$ arch_flags="-arch i386"
$ ./configure CFLAGS="$arch_flags" CXXFLAGS="$arch_flags" CPPFLAGS="$arch_flags" \
  LDFLAGS="$arch_flags" OBJCFLAGS="$arch_flags" OBJCXXFLAGS="$arch_flags" \
  --with-readline=builtin --with-gd=/usr/local/lib --with-png=/usr/local/lib \
  --with-plot=/usr/local/lib --x-includes=/usr/include/X11 \
  --x-libraries=/usr/X11/lib --with-lua --with-pdf=/usr/local/lib
$ make
$ sudo make install

It should be noted that I used --with-readline=builtin and not the GNU libreadline I installed separately. It generated some unexpected conflicts with lua, although I don’t really know why. If everything went fine, you should see at the end of the configure step:1

** Configuration summary for gnuplot 4.5:

gnuplot will be compiled with the following terminals:

  Standalone terminals: yes (always builtin)
    canvas, cgm, corel, dumb, dxf, eepic, emf, emtex,
    epslatex, fig, gpic, hp2623a, hp2648, hpgl, imagen,
    latex, metafont, metapost, mif, pcl5, postscript, pslatex,
    pstex, pstricks, qms, regis, svg,
    tek40xx, tek410x, texdraw, tgif, tkcanvas, tpic, vttek

  dot-matrix terminals: yes (disable using --without-bitmap-terminals
    NB: copyright may restrict commercial use
    epson, nec, okidata, tandy, and seiko dp414 printers
    hp500c, hpdj, hpljii, hppj, pbm, starc

  X Window System terminal: yes
    (with multi-byte fonts)
    (enable plotting to windows opened by external apps) 
    (without binary polygon protocol )
    (with application defaults, in /etc/X11/app-defaults/)
  jpeg terminal: no (requires libgd with jpeg support)
  gif terminal: no (requires libgd with gif support)
  png terminal: no (requires libgd with png support)
    (jpeg, gif and png terminals cannot use TTF fonts, requires libgd support)
  pdf terminal: no (requires libpdf)
  plot library terminal: no (requires GNU plotutils or UNIX plot)
  linux terminal (vga console): no (use --with-linux-vga to enable,
                                       requires SVGAlib)
  vgagl terminal ((s)vga console): no (use --with-linux-vga to enable,
                                       requires SVGAlib with vgagl)
  ggi terminal: no (use --with-ggi to enable, requires libggi)
  mgr terminal: no (use --enable-mgr to enable, requires libpixrect)
  rgip/uniplex terminal: no (use --enable-rgip to enable)
  svga terminal (MSDOS/djgpp): no
  be terminal (BeOS): no
  next terminal: no
  aqua terminal (MacOS X): yes
  wxt terminal: no (requires C++, wxWidgets>2.6, cairo>0.9, pango>1.10)
  cairo-based terminals: no (requires cairo>1.2, pango>1.10)
  lua/TikZ terminal: yes 
  Qt terminal: no (requires C++, Qt >= 4.5)

...

gnuplot will be compiled with the following features:

  Mouse support in interactive terminals: yes
  Zooming or refresh of volatile data: yes (EXPERIMENTAL)
  Typing <space> in plot window raises console
  Command line macros: yes 
  Placement of rectangles and other objects: yes 

Finally, after the make process, we should get:

test -z "BUILD_LUA" || lua ../../term/lua/gnuplot-tikz.lua style
cp -p ./Gnuplot.app-defaults Gnuplot
make[2]: Nothing to be done for `all-am'.

That’s it! Running gnuplot at my bash prompt shows:

$ gnuplot

    G N U P L O T
    Version 4.5 patchlevel 0
    last modified 2011-05-07 
    System: Darwin 10.7.0

    Copyright (C) 1986-1993, 1998, 2004, 2007-2010
    Thomas Williams, Colin Kelley and many others

    gnuplot home:     http://www.gnuplot.info
    mailing list:     gnuplot-beta@lists.sourceforge.net
    faq, bugs, etc:   type "help seeking-assistance"
    immediate help:   type "help"
    plot window:      hit 'h'

Terminal type set to 'x11'
gnuplot> set term aqua
Terminal type set to 'aqua'
Options are '0 title "Figure 0" size 846,594 font "Times-Roman,14" noenhanced solid'
gnuplot> set terminal lua tikz fulldoc createstyle
Terminal type set to 'lua'
Options are 'latex nopreamble color standalone dashed nogparrows notikzarrows \
  nogppoints picenvironment nooriginreset bitmap rgbimage noclip '
gnuplot> 

Finally, let’s consider those commands:

set xrange [-2:2]
set yrange [-2:2]
splot exp(-x*x)*exp(-y*y)

and see how they are rendered with x11, aqua, and tikz:

To polish the installation, you can move the demo folder into /usr/local/share/gnuplot/demo (or wherever you want) and add

export GNUPLOT_LIB="/usr/local/share/gnuplot/demo"

to your .profile in your home directory. This will allow to launch every example file, but also the Library of Statistical Functions version (3.0), stat.inc, which includes a lot of PDF and CDF definitions for common statistical distribution. If you use Emacs, be sure to add relevant lisp files into your load path, and to put the next instructions into you .emacs:

(setq load-path (append (list "/Users/chl/.lisp.d/gnuplot") load-path))
(autoload 'gnuplot-mode "gnuplot" "gnuplot major mode" t)
(autoload 'gnuplot-make-buffer "gnuplot" "open a buffer in gnuplot mode" t)
(setq auto-mode-alist (append '(("\\.gp$" . gnuplot-mode)) auto-mode-alist))

The gnuplot-mode is really great, for it allows to interact with Gnuplot directly from within Emacs while you type in your code, including minimal GUI functionalities.


  1. I will probably have to upgrade my GD installation to get the png and jpg terminal. ↩︎

See Also

» GIS on a Mac » Visualizing What Random Forests Really Do » Yet another interactive shell for numerical computing » Playing with BackupMyTweets » Circular displays for contingency tables