Difference between revisions of "Gnuplot"

From Wiki
Jump to navigation Jump to search
(Category:Math instead of Mathematics, todo tag, link)
(reflects the recent changes in the gnuplot module)
Line 3: Line 3:
 
[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.
  
== Preliminary Modules ==
+
[[source:m-gnuplot.tex|m-gnuplot.tex]]: the source file ''(contextgarden is not updated to the latest version yet, so you may not be able to see this)''
  
Some preliminary modules were contributed by Peter Münster [http://article.gmane.org/gmane.comp.tex.context/24884] and Hans Hagen [http://article.gmane.org/gmane.comp.tex.context/24935] [http://article.gmane.org/gmane.comp.tex.context/24965] on the mailing list to enable inclusion of gnuplot graphs from ConTeXt. Follow the links mentioned above to see the details. General idea is to enable gnuplot graphics inclusion with simple commands such as:
+
After Peter Münster posted the first module for gnuplut support on the mailing list [http://article.gmane.org/gmane.comp.tex.context/24884] (which only works with bash), Hans Hagen (see [http://article.gmane.org/gmane.comp.tex.context/24935] [http://article.gmane.org/gmane.comp.tex.context/24965]) 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).
  
 +
== 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
 +
<texcode>
 +
\setupGNUPLOT[output=mp,options=color solid]
 +
</texcode>
 +
 +
* <code>output=</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>
 
<texcode>
% general settings for the whole document
 
 
\startGNUPLOTinclusions
 
\startGNUPLOTinclusions
set terminal postscript eps color
+
set terminal mp color solid
set title 'Trigonometry'
 
 
\stopGNUPLOTinclusions
 
\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:
 +
* [http://www.gnuplot.info/docs/gnuplot.html#mp mp/metapost]: <b>most beautiful results</b>, uses [[MPtoPDF]] to convert images to pdf
 +
* [http://www.gnuplot.info/docs/gnuplot.html#postscript_ ps/postscript]: uses newpstopdf to convert images to pdf; ''currently some problems with rotated images unless <code>option=eps</code> is used''
 +
* [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
 +
 +
Multiple plots (more images/pages of output) are not supported (there's probably no reason to) since you can split the graphs in subsequent <code>\startGNUPLOTgraphic ... \stopGNUPLOTgraphic if necessary</code>.
 +
 +
=== Minimal Example ===
 +
 +
<texcode>
 +
\usemodule[gnuplot]
  
 
% creating the graphics
 
% creating the graphics
 
\startGNUPLOTgraphics{sin}
 
\startGNUPLOTgraphics{sin}
plot sin(x)
+
  plot sin(x)
 
\stopGNUPLOTgraphics
 
\stopGNUPLOTgraphics
  
 
% including the graphics into the document
 
% including the graphics into the document
\useGNUPLOTgraphic[sin][width=5cm]
+
\useGNUPLOTgraphic[sin]
 
</texcode>
 
</texcode>
  
''Note: Peter's module currently only works under Linux, Hans's module has problems with newlines. Hoping that that will be fixed ...''
+
=== 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.
 +
 
 +
<texcode>
 +
\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]
 +
</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).
 +
 
 +
=== More complex example: Multiple terminals ===
 +
 
 +
You can use different terminals for plotting the same function.
 +
 
 +
{{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}}
 +
 
 +
== 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 <code>pgnuplot.bat</code> with
 +
"C:\Program Files\gnuplot\wgnupl32.exe" %1 %2 %3 %4 %5 %6 %7 %8 %9
 +
and put that file to a <i>"visible place"</i> (has to be found in PATH).
 +
 
 +
For most terminals you need <b>texmfstart</b>(.exe) as well. In case you don't have it yet, you can grab it [http://www.pragma-ade.com/context/current/cont-win.zip here].
 +
 
 +
[[write18]] has to be enabled as well.
  
 
== Alternatives for Function Plotting in ConTeXt ==
 
== Alternatives for Function Plotting in ConTeXt ==
Line 30: Line 110:
 
** [[source:m-graph.tex|m-graph]] Module (see [http://cm.bell-labs.com/who/hobby/cstr_164.pdf Drawing Graphs with MetaPost] by John D. Hobby)
 
** [[source:m-graph.tex|m-graph]] Module (see [http://cm.bell-labs.com/who/hobby/cstr_164.pdf Drawing Graphs with MetaPost] by John D. Hobby)
 
** link to functions that Hans wrote in MetaFun
 
** link to functions that Hans wrote in MetaFun
 
{{todo|
 
* Fix OS-specifics to make the module work
 
* Support multiple terminals
 
* ...}}
 
  
 
[[Category:Modules]]
 
[[Category:Modules]]
 
[[Category:Graphics]]
 
[[Category:Graphics]]
 
[[Category:Math]]
 
[[Category:Math]]

Revision as of 04:13, 15 January 2006

< Modules | Graphics | Math >

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

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. 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