Gnuplot

From Wiki
Revision as of 00:56, 12 August 2006 by Mojca Miklavec (talk | contribs) (a slight revision reflecting the current state; needs more changes, but not today any more)
Jump to navigation Jump to search

< Modules | Graphics | Math >

Gnuplot is a portable command-line driven utility for function plotting for many platforms.


TODO: The first Hans's version of the module soon became a part of the official distribution (m-gnuplot.tex), but please, remain patient for some time since there have been made quite some important changes that didn't come to the official distribution yet. If you would like to use the module now, please contact me directly or ask on the mailing list.

All the examples on this page talk about how the things are supposed to work once the new terminal is uploaded. The code described doesn't work yet unless you download new files from the darcs repository (see below). (See: To-Do List)


Minimal Example

\usemodule[gnuplot]

% write a script for gnuplot
\startGNUPLOTscript{sin}
   plot sin(x)
\stopGNUPLOTscript

% include the resulting graphic into the document
\useGNUPLOTgraphic[sin]

Calling gnuplot to create the graphic and postprocessing should happen automatically.

Requirements

  • Gnuplot has to be installed (and in your PATH)
On most Linux installations this is probably already the case. On Windows you have to create a file gnuplot.bat with something like:
"C:\Program Files\gnuplot\wgnupl32.exe" %*

and put that file to a "visible place" (has to be found in PATH).

  • recent version of ConTeXt and working texmfstart
  • if you want to use the ConTeXt terminal, the latest gnuplot is not enough
    • The ConTeXt terminal is not part of official gnuplot version (yet), so if you want to try it on Linux, you have to download the source from http://www.gnuplot.info, add context.trm, fix the file term.h and compile it.
    • Under Windows you can use these binaries (they're not perfect though - PDF and PNG/GIF terminals are not working properly). Until the terminal becomes a part of the official GNUPLOT version it might be a bit of additional work to install it. Please have understanding for it.

Terminals

Gnuplot support output in different formats with, for example

set terminal mp color solid

will output a metapost document with colors and without dashed lines. After some appropriate preprocessing (with MPtoDF for this particular case) this file can be included in PDF documents.

You can specify the terminal with

\setupGNUPLOT[terminal=mp,options=color solid]
  • terminal= is mandatory since that't the only way to tell ConTeXt how to preprocess the file.
  • options= is optional and is appended to the end of set terminal [terminal name]. You can also override it by saying
\startGNUPLOTinclusions
set terminal mp color solid
\stopGNUPLOTinclusions

But you have to be careful that you don't set any other terminal with output= then.


Currently supported terminals are:

  • mp/metapost: most beautiful results, uses MPtoPDF to convert images to pdf, but the context terminal should replace it
  • ps/postscript: most advanced in the number of supported features
  • png: outputs bitmap images (no preprocessing needed)
  • pdf: outputs PDF directly (no preprocessing needed), but is usually not available in most binaries of gnuplot
  • context: new

More complex example with MetaPost or ConTeXt terminal

Metapost produces the plots of a relatively high quality. You can use TeX commands to format the title, label axes and legend.

\usemodule[gnuplot]

% output may be "mp/metapost", "ps/postscript", "pdf" or "png"; ps is currently the default
\setupGNUPLOT[terminal=mp,option=color]

% general settings for the whole document
\startGNUPLOTinclusions

   # you may use TeX commands to format the titles and axes
   set title '\bf Trigonometry'
   set xlabel '$x$'
   set ylabel '$y$'

   # to prevent uneven numbering: will result in
   # (0.0, 0.5, 1.0, 1.5, ...) instead of
   # (0, 0.5, 1, 1.5, ...)
   set format yaxis '\%.1f'
\stopGNUPLOTinclusions

\startGNUPLOTscript{tan}
   plot tan(x) t '$\tan(x)$'
\stopGNUPLOTscript

\startGNUPLOTscript{sin}
   plot 4*sin(x)+x t '$4\sin(x)+x$', x t '$x$' lt 3
\stopGNUPLOTscript

\useGNUPLOTgraphic[tan]
\useGNUPLOTgraphic[sin][width=10cm]

There's only one problem that you have to be careful about: MetaPost terminal uses cmr fonts for labels. If you have the stand-alone distributions, you either have to install them by yourself or to play with set terminal mp {fontname} a bit (http://www.gnuplot.info/docs/gnuplot.html#mp).

More complex example: Multiple terminals

You can use different terminals for plotting the same function.


TODO: an example, similar to the one in m-gnuplot.tex, some images to get a visual impression of the main differences in terminals (See: To-Do List)


Development

History

I (Mojca Miklavec) probably mentioned long time ago that I would like to have (or write?) support for Gnuplot inside ConTeXt, but I had no knowledge to do that. Peter Münster posted the very first module for gnuplut support on the mailing list [1], but that version only worked with bash. In the days to follow Hans Hagen has written a new module (based on my numerous "possible and impossible" requests) and Taco additionally provided some bugfixes and minor changes.

The module provides an option to select the most appropriate/suitable driver according to the user's choice (metapost, png, pdf, postscript were the firs ones to be supported). Hans was wondering why there was no context terminal. I took over it's development, the code is almost finished by now, but I guess that Hans now regrets that question already.

He had to invest quite some time into fixing buggy inclusion of text into graphics using textext (if nothing else, he had to read and reply to all the mails [complaints] that me and Taco sent him), just to lead to the conclusion that textext was useless for graphics produced by Gnuplot: too slow and TeX ran out of memory after 10 minutes while processing approximately the 13th plot. After that the new marvellous \sometxt command was born.

I'm currently sticking tiny pieces together in the way I want the module to work. Hans could probably finish the same work that costs me a few days in a few minutes, but after dropping me a bone, he decided to leave me the joy of diving into ConTeXt internals and to let me figure out how to program in ConTeXt by myself. So I still keep spamming the mailing list with numerous questions and both Taco and Hans - without whom implementing the module would be impossible - keep answering.

Alternatives for Function Plotting

  • in MetaPost
  • others
    • PyX - graph drawing through python interface with TeX typesetting & PostScript capabilities
    • Tioga - creating figures and plots using Ruby, PDF, and TeX
    • mfpic - a scheme for producing pictures from LaTeX commands. The LaTeX commands generate a input file to be processed by Metapost.

Other links

  • exceltex - an interesting LaTeX package to get data from Excel into LaTeX