Gnuplot

From Wiki
Revision as of 00:14, 23 July 2006 by Mojca Miklavec (talk | contribs) (→‎Requirements: note about the context terminal under requirements)
Jump to navigation Jump to search

< Modules | Graphics | Math >

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

m-gnuplot.tex: the source file

After Peter Münster posted the first module for gnuplut support on the mailing list [1] (which only works with bash), Hans Hagen (see [2] [3]) wrote another one and Taco additionally provided some bugfixes and minor changes. The module is now a part of the official distribution, but may undergo some other minor changes in the short term (use with care for some time).

Development

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[output=mp,options=color solid]
  • output= 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
  • ps/postscript: uses newpstopdf to convert images to pdf; currently some problems with rotated images unless option=eps is used
  • png: outputs bitmap images (no preprocessing needed)
  • pdf: outputs PDF directly (no preprocessing needed), but is usually not available in most binaries of gnuplot

Multiple plots (more images/pages of output) are not supported (there's probably no reason to) since you can split the graphs in subsequent \startGNUPLOTgraphic ... \stopGNUPLOTgraphic if necessary.

Minimal Example

\usemodule[gnuplot]

% creating the graphics
\startGNUPLOTgraphics{sin}
   plot sin(x)
\stopGNUPLOTgraphics

% including the graphics into the document
\useGNUPLOTgraphic[sin]

More complex example with MetaPost

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[output=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

\startGNUPLOTgraphics{tan}
   plot tan(x) t '$\tan(x)$'
\stopGNUPLOTgraphics

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

\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)


Requirements

You have to have Gnuplot installed (and in your PATH). On most Linux installations this is probably already the case. On Windows you either need to put the gnuplot binary in PATH manually or to create a file pgnuplot.bat with

"C:\Program Files\gnuplot\wgnupl32.exe" %1 %2 %3 %4 %5 %6 %7 %8 %9

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

For most terminals you need texmfstart(.exe) as well.

ConTeXt terminal

If you want to use the (native) context terminal, gnuplot has to support it. That 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.

Note: some (slightly incompatible) changes in the interface are to be expected soon. I can tell you which ones exactly, but they're not implemented in the module yet. After the module will be anounced to be stable (hopefully before Autumn this year) they'll stay as they are. Until then please use the module with some care.

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