Gnuplot
Gnuplot is a portable command-line driven utility for function plotting for many platforms.
m-gnuplot.tex: the source file (contextgarden is not updated to the latest version yet, so you may not be able to see this)
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).
Contents
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 ofset 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. In case you don't have it yet, you can grab it here.
write18 has to be enabled as well.
Alternatives for Function Plotting in ConTeXt
- in MetaPost
- m-graph Module (see Drawing Graphs with MetaPost by John D. Hobby)
- link to functions that Hans wrote in MetaFun