Tutorial

High-quality plots with Octave

Executive Summary

  • What follows has been tested with Octave 3.6, TexLive 2011, and gnuplot 4.6, on Linux.
  • What follows should work with Matlab, maybe with minor modifications. I don't have Matlab.
  • gnuplot should be used for what it's better at: creating useful, pretty plots on screen.
  • Latex should be used for what it's better at: creating high-quality documents, including plots (with the pgfplots package).
  • I present a script, called printpgf, that integrates Octave with pgfplots to painlessly create high-quality plots. Available here.

Detailed description

Sometimes one needs to produce a document with high-quality typography and plots, for instance to publish in a magazine. The typography problem is easily solved with Latex. Plots may be produced with Octave (or Matlab). As an example, assume you have produced three vectors x, y1 and y2:

x = [1 2 3 4 5 6 7 8 9 10];
y1 = [.16 .08 .04 .02 .013 .007 .004 .002 .001 .0008 ];
y2 = [.16 .07 .03 .01 .008 .003 .0008 .0003 .00007 .00002 ];

and a plot of y1 and y2 versus x is desired. The vertical axis must be logarithmic, the curve of y1 must be blue with round markers, and y2's must be black with cross-like markers. The plot must be titled, and the axes must be labeled. Also, a legend must be present to identify each variable. The plot must be saved in PDF format for easy inclusion in the document. One may do the following in Octave:

semilogy(x,y1,'-bo;y1;',x,y2,'-kx;y2;');
title('Plot title');
xlabel('X Axis');
ylabel('Y Axis');
print figure.pdf

The obtained PDF file is similar to this:

which is hard to consider a high quality plot. Among other problems, font size and type will be fixed in the PDF file and very likely different from the rest of the document.

It is possible to tweak and fine-tune many aspects of the plot. With Octave 3.6, it is even possible to export the plot in TiKZ format, which is capable of beautiful graphics. In this case, the result looks like this:

This is clearly better, but I don't think it's publication quality yet. The plot's appearance can be substantially improved setting various gnuplot options. This takes time and, in some cases, it may be impossible to obtain a particular look. It should be noted that gnuplot's tikz terminal is designed to create a Latex figure that mimics, as closely as possible, what you would see on-screen with a graphical terminal (with the exception of fonts, which integrate nicely with the rest of the document).

I believe this method is ultimately suboptimal, besides being limited to what gnuplot can do. The good news is that there is a better method: keep using gnuplot for visualizing plots on the screen, but use the pgfplots Latex package for output to PDF. In other words, use the best tool for each job. The workflow would be as follows:

  1. Use Octave code to produce one or more sets of X, Y coordinates.
  2. Use Octave's plot, stem and related commands to quickly assess the results on screen.
  3. Once satisfied, use pgfplots to produce high-quality PDF plots.

How to use pgfplots, ideally from within Octave, and with print's convenience and ease of use? With the printpgf script. This code:

c.standalone = '1';
c.axistype = 'semilogyaxis';
c.xlabel = 'X Axis';
c.ylabel = 'Y Axis';
c.title = 'Plot title';
c.runtex = 1;
p.x = x;
p.y = y1;
p.color = 'blue';
p.mark = 'o';
p.legend = 'y1';
P{1} = p;
p.y = y2;
p.color = 'black';
p.mark = 'x';
p.legend = 'y2';
P{2} = p;
printpgf(c,P);

produces this plot:

which, to my eyes at least, is much nicer and cleaner. Furthermore, the pgfplots package is extremely powerful and flexible, with capabilities that are well beyond gnuplot's (read the whole manual here). Not all of this power is accessible from the printpgf script, but quite a lot of options are available. This script is available as an Octave package here. The script itself is well documented. Matlab users should be able to grab the script and run it with minor modifications, if at all (I don't have access to Matlab).

Please send bug reports, suggestions or comments to the address listed below.

Appendix

In order to use the tikz terminal from Octave, you need to do the following. First, generate your plot and tweak it until it looks as desired. Then do

print -dtikz tikz.tex

This command will generate (or overwrite) the file tikz.tex. This file is designed to be included in a Latex document. A minimum example would look like this:

\documentclass[10pt]{article}

\usepackage[T1]{fontenc}
\usepackage{textcomp}
\usepackage[utf8x]{inputenc}
\usepackage{gnuplot-lua-tikz}
\usepackage[active,tightpage]{preview}

\pagestyle{empty}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{\gpbboxborder}

\begin{document}
\include{tikz}
\end{document}

This file may be compiled, for instance, with pdflatex. Note the include line near the end; the file included must be the one created from Octave.