Difference between revisions of "Gnuplot"

From Wiki
Jump to navigation Jump to search
(some updates (not yet finished))
(22 intermediate revisions by 9 users not shown)
Line 5: Line 5:
 
[http://www.gnuplot.info Gnuplot] is a portable command-line driven utility for function plotting for many platforms.
 
[http://www.gnuplot.info Gnuplot] is a portable command-line driven utility for function plotting for many platforms.
  
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).}}
+
To make the examples on this page work, you need Gnuplot 4.6.0 or later (or self-compiled binary).
  
 
== Minimal Example ==
 
== Minimal Example ==
Line 13: Line 13:
  
 
% write a script for gnuplot
 
% write a script for gnuplot
\startGNUPLOTscript{sin}
+
\startGNUPLOTscript[sin]
 
   plot sin(x)
 
   plot sin(x)
 
\stopGNUPLOTscript
 
\stopGNUPLOTscript
Line 25: Line 25:
 
== Requirements ==
 
== Requirements ==
  
* '''recent version of ConTeXt''' and '''texmfstart''' in your PATH
+
* '''[http://modules.contextgarden.net/gnuplot Gnuplot module]'''
* '''[http://modules.contextgarden.net/gnuplot Gnuplot module]''' (on MikTeX it's part of the distribution already)
+
** in TeX Live you need to have <code>context-gnuplot</code> installed, in [[ConTeXt Standalone]] you can install it with <code>--modules=t-gnuplot</code> switch
 
* '''Gnuplot has to be installed''' (and in your PATH)
 
* '''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 <code>gnuplot.bat</code> with something like:
+
:On most Linux installations this is probably already the case. On Windows gnuplot.exe is usually also shipped and you need to make sure that it is in PATH. In case that the binary has a different name, you could create a file <code>gnuplot.bat</code> with something like:
  "C:\Program Files\gnuplot\wgnupl32.exe" %*
+
  "C:\Program Files\gnuplot\bin\wgnupl32.exe" %*
and put that file to a <i>"visible place"</i> (has to be found in PATH).
+
:(if that was the binary name) and put that file to a <i>"visible place"</i> (has to be found in PATH). Please note: if calling <code>gnuplot</code> from cmd works for you, there is no need to do anything extra.
* if you want to use the ConTeXt terminal (default), '''you need modified binaries''' until they officially become part of gnuplot
 
** if you want to try it on Linux, you have to download the source from http://www.gnuplot.info, add [http://renojrl.lrv.uvsq.fr/projects/gpcontext/term/context.trm context.trm], fix the file <code>term.h</code> and compile it.
 
** For windows version of binary or if you have problems compiling it, please mail me to <mojca.miklavec.lists(at)gmail.com>
 
 
* '''[[write18]]''' has to be enabled
 
* '''[[write18]]''' has to be enabled
 +
=== To use the ConTeXt terminal (recommended) ===
 +
====  Windows ====
 +
You can fetch binaries from [http://sourceforge.net/projects/gnuplot/files/gnuplot/ sourceforge (released versions)] or from [http://www.tatsuromatsuoka.com/gnuplot/Eng/winbin/ Tatsuro Matsuoka (latest cvs version)].
 +
====  Unix or Mac ====
 +
The easiest way is to use version 4.6.0 or later shipped by your distribution. If that version is too old, you need to compile your own.
  
<i>The module has some strange problems when used on MikTeX (memory limit exceeded or something similar). I'll try to figure out what exactly is going on.</i> --[[User:Mojca Miklavec|Mojca]]
+
If you want to use the version from trunk:
 +
# unofficial; you can also use official cvs
 +
git clone git://github.com/gnuplot/gnuplot.git
 +
./prepare
 +
./configure
 +
make
 +
make install
 +
I use <code>./configure --prefix=$PWD/inst</code> to avoid cluttering the system and install gnuplot to my personal directory. You can use other flags to configure for a different choice of GUI terminals.
  
== Terminals ==
+
If you want to use the released version, get it from [http://sourceforge.net/projects/gnuplot/files/gnuplot/ sourceforge] and do the same, just skip the <code>./prepare</code>.
  
Gnuplot support output in different formats with, for example
+
The latest version of context terminal is available at http://github.com/mojca/gnuplot/raw/master/term/context.trm.
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
+
Feel free to contact [[User:Mojca Miklavec|Mojca]] in case of problems.
<texcode>
 
\setupGNUPLOT[terminal=mp,options=color solid]
 
</texcode>
 
 
 
* <code>terminal=</code> is mandatory since that't the only way to tell ConTeXt how to preprocess the file.
 
* <code>options=</code> is optional and is appended to the end of <code>set terminal [terminal name]</code>. You can also override it by saying
 
<texcode>
 
\startGNUPLOTinclusions
 
set terminal mp color solid
 
\stopGNUPLOTinclusions
 
</texcode>
 
But you have to be careful that you don't set any other terminal with <code>output=</code> then.
 
  
<!-- Another way to specify it is to override the terminal setting  -->
+
== Creating stand-alone, whole-page plots ==
  
Currently supported terminals are:
+
Here's an example of how to make a stand-alone ConTeXt document with one plot per page:
* [http://www.gnuplot.info/docs/gnuplot.html#mp mp/metapost]: <b>most beautiful results</b>, uses [[MPtoPDF]] to convert images to pdf, but the context terminal should replace it
 
* [http://www.gnuplot.info/docs/gnuplot.html#postscript_ ps/postscript]: most advanced in the number of supported features
 
* [http://www.gnuplot.info/docs/gnuplot.html#png_(OLD) png]: outputs bitmap images (no preprocessing needed)
 
* [http://www.gnuplot.info/docs/gnuplot.html#pdf 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 ===
+
set term context standalone size 15cm,10cm header '\usetypescript[iwona]' font 'iwona,ss' 10dd
 +
set output 'example.tex'
 +
plot ...
  
Metapost produces the plots of a relatively high quality. You can use TeX commands to format the title, label axes and legend.
+
== More complex example ==
  
 
<texcode>
 
<texcode>
 
\usemodule[gnuplot]
 
\usemodule[gnuplot]
  
% output may be "mp/metapost", "ps/postscript", "pdf" or "png"; ps is currently the default
+
\setupGNUPLOT[terminal=tikz,option=color] % terminal=mp if you only have an old gnuplot at hand
\setupGNUPLOT[terminal=mp,option=color]
 
  
 
% general settings for the whole document
 
% general settings for the whole document
Line 88: Line 79:
 
   # (0.0, 0.5, 1.0, 1.5, ...) instead of
 
   # (0.0, 0.5, 1.0, 1.5, ...) instead of
 
   # (0, 0.5, 1, 1.5, ...)
 
   # (0, 0.5, 1, 1.5, ...)
   set format y '\%.1f'
+
   set format y '%.1f'
 
\stopGNUPLOTinclusions
 
\stopGNUPLOTinclusions
  
\startGNUPLOTscript{tan}
+
\startGNUPLOTscript[tan]
 
   plot tan(x) t '$\tan(x)$'
 
   plot tan(x) t '$\tan(x)$'
 
\stopGNUPLOTscript
 
\stopGNUPLOTscript
  
\startGNUPLOTscript{sin}
+
\startGNUPLOTscript[sin]
 
   plot 4*sin(x)+x t '$4\sin(x)+x$', x t '$x$' lt 3
 
   plot 4*sin(x)+x t '$4\sin(x)+x$', x t '$x$' lt 3
 
\stopGNUPLOTscript
 
\stopGNUPLOTscript
Line 103: Line 94:
 
</texcode>
 
</texcode>
  
There's only one problem that you have to be careful about: [[MetaPost]] terminal uses <code>cmr</code> fonts for labels. If you have the stand-alone distributions, you either have to install them by yourself or to play with <code>set terminal mp {fontname}</code> a bit (http://www.gnuplot.info/docs/gnuplot.html#mp).
+
== Using other terminals ==
  
=== More complex example: Multiple terminals ===
+
<i>(Needs a revision.)</i>
  
You can use different terminals for plotting the same function.
+
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 [[MPtoPDF]] for this particular case) this file can be included in PDF documents.
  
{{todo|an example, similar to the one in [[source:m-gnuplot.tex|m-gnuplot.tex]], some images to get a visual impression of the main differences in terminals}}
+
You can specify the terminal with
 +
<texcode>
 +
\setupGNUPLOT[terminal=mp,options=color solid]
 +
</texcode>
  
== Development ==
+
* <code>terminal=</code> is mandatory since that't the only way to tell ConTeXt how to preprocess the file.
 +
* <code>options=</code> is optional and is appended to the end of <code>set terminal [terminal name]</code>. You can also override it by saying
 +
<texcode>
 +
\startGNUPLOTinclusions
 +
set terminal mp color solid
 +
\stopGNUPLOTinclusions
 +
</texcode>
 +
But you have to be careful that you don't set any other terminal with <code>output=</code> then.
 +
 
 +
<!-- Another way to specify it is to override the terminal setting  -->
 +
 
 +
Currently supported terminals are (attention: links not from the latest documentation):
 +
* [http://www.gnuplot.info/docs/gnuplot.html#mp mp/metapost]: uses [[MPtoPDF]] to convert images to pdf, but the context terminal should replace it
 +
* [http://www.gnuplot.info/docs/gnuplot.html#postscript_ ps/postscript]: most advanced in the number of supported features
 +
* [http://www.gnuplot.info/docs_4.0/gnuplot.html#png_%28NEW%29 png (1)],[http://www.gnuplot.info/docs_4.2/gnuplot.html#x1-41500051.53 png (2)]: outputs bitmap images (no preprocessing needed)
 +
* [http://www.gnuplot.info/docs_4.0/gnuplot.html#pdf pdf (old)],[http://www.gnuplot.info/docs_4.2/gnuplot.html#x1-41300051.51 pdf (new)]: outputs PDF directly for (no preprocessing needed), but is usually not available in most binaries of gnuplot
 +
* '''context''':
  
* [http://renojrl.lrv.uvsq.fr/darcsweb.cgi?r=gpConTeXt;a=summary darcs repository] (maintained by Renaud Aubin)
+
The current gnuplot-documentation can be downloaded [http://www.gnuplot.info/docs_4.6/gnuplot.pdf here].
* [http://groups.google.com/group/gnuplot-context mailing list]
 
  
 
== History ==
 
== History ==
Line 135: Line 146:
 
** [http://theory.kitp.ucsb.edu/~paxton/tioga.html Tioga] - creating figures and plots using Ruby, PDF, and TeX
 
** [http://theory.kitp.ucsb.edu/~paxton/tioga.html Tioga] - creating figures and plots using Ruby, PDF, and TeX
 
** [http://comp.uark.edu/~luecking/tex/mfpic.html mfpic]  - a scheme for producing pictures from LaTeX commands. The LaTeX commands generate a input file to be processed by Metapost.
 
** [http://comp.uark.edu/~luecking/tex/mfpic.html mfpic]  - a scheme for producing pictures from LaTeX commands. The LaTeX commands generate a input file to be processed by Metapost.
 +
** [http://matplotlib.sourceforge.net matplotlib]  - a Matlab-inspired Python based plotting tool which incorporates many features, including the use of TeX in labels, titles etc.
 +
** [http://tug.ctan.org/tex-archive/graphics/pgf/contrib/pgfplots/ pgfplots] - a TikZ-based LaTeX package for drawing graphs
  
 
=== Other links ===
 
=== Other links ===
 
* [http://tug.ctan.org/tex-archive/macros/latex/contrib/exceltex/ exceltex] - an interesting LaTeX package to get data from Excel into LaTeX
 
* [http://tug.ctan.org/tex-archive/macros/latex/contrib/exceltex/ exceltex] - an interesting LaTeX package to get data from Excel into LaTeX
  
 +
[[Image:Slo Ministry of Higher Education Science and Technology.jpg]]
  
 
[[Category:Modules]]
 
[[Category:Modules]]
 
[[Category:Graphics]]
 
[[Category:Graphics]]
 
[[Category:Math]]
 
[[Category:Math]]

Revision as of 09:43, 11 April 2012

< Modules | Graphics | Math >

http://www.gnuplot.info/figs/title2.png

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

To make the examples on this page work, you need Gnuplot 4.6.0 or later (or self-compiled binary).

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 module
    • in TeX Live you need to have context-gnuplot installed, in ConTeXt Standalone you can install it with --modules=t-gnuplot switch
  • Gnuplot has to be installed (and in your PATH)
On most Linux installations this is probably already the case. On Windows gnuplot.exe is usually also shipped and you need to make sure that it is in PATH. In case that the binary has a different name, you could create a file gnuplot.bat with something like:
"C:\Program Files\gnuplot\bin\wgnupl32.exe" %*
(if that was the binary name) and put that file to a "visible place" (has to be found in PATH). Please note: if calling gnuplot from cmd works for you, there is no need to do anything extra.

To use the ConTeXt terminal (recommended)

Windows

You can fetch binaries from sourceforge (released versions) or from Tatsuro Matsuoka (latest cvs version).

Unix or Mac

The easiest way is to use version 4.6.0 or later shipped by your distribution. If that version is too old, you need to compile your own.

If you want to use the version from trunk:

# unofficial; you can also use official cvs
git clone git://github.com/gnuplot/gnuplot.git
./prepare
./configure
make
make install

I use ./configure --prefix=$PWD/inst to avoid cluttering the system and install gnuplot to my personal directory. You can use other flags to configure for a different choice of GUI terminals.

If you want to use the released version, get it from sourceforge and do the same, just skip the ./prepare.

The latest version of context terminal is available at http://github.com/mojca/gnuplot/raw/master/term/context.trm.

Feel free to contact Mojca in case of problems.

Creating stand-alone, whole-page plots

Here's an example of how to make a stand-alone ConTeXt document with one plot per page:

set term context standalone size 15cm,10cm header '\usetypescript[iwona]' font 'iwona,ss' 10dd
set output 'example.tex'
plot ...

More complex example

\usemodule[gnuplot]

\setupGNUPLOT[terminal=tikz,option=color] % terminal=mp if you only have an old gnuplot at hand

% 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 y '%.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]

Using other terminals

(Needs a revision.)

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 MPtoPDF 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 (attention: links not from the latest documentation):

  • mp/metapost: 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 (1),png (2): outputs bitmap images (no preprocessing needed)
  • pdf (old),pdf (new): outputs PDF directly for (no preprocessing needed), but is usually not available in most binaries of gnuplot
  • context:

The current gnuplot-documentation can be downloaded here.

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.
    • matplotlib - a Matlab-inspired Python based plotting tool which incorporates many features, including the use of TeX in labels, titles etc.
    • pgfplots - a TikZ-based LaTeX package for drawing graphs

Other links

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

Slo Ministry of Higher Education Science and Technology.jpg