You are on page 1of 9

A L TEX and the Gnuplot Plotting Program

David Kotz Principal author of this tutorial for gnuplot 3.0, July 3, 1991

current gnuplot team Updates of this tutorial for gnuplot 4.0, March 2004 Update of this tutorial for gnuplot 4.2, August 2006 Update of this tutorial for gnuplot 4.4, September 2010

1 Introduction and History
A 2 Using gnuplot for L TEX: a Tutorial A 2.1 Summary — Use with L TEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

1 1 5 6 7 7 7 7

3 Use with EEPIC
A 4 What is new for TEX and L TEX terminals in gnuplot 4.0 A 5 What is new for TEX and L TEX terminals in gnuplot 4.4

6 Sample of epslatex terminal driver 7 Contact for help

For example.0. we direct the output explicitly with an instruction to gnuplot (the set output "outfile.4 1 1 Introduction and History Gnuplot was originally developed by Colin Kelley and Thomas Williams in 1986 to plot functions and data files on a variety of terminals.tex" plot [-3. and plots y = sin(x) for x on [−π. 1 In gnuplot version 4.shar from cs. I simply \input{eg1} in a center environment in a figure environment. all of the commands to gnuplot are contained in the file plotcommands. i. gives a file name for the output. After printing.. A X faster. the program need not be run on a graphics terminal. If an enlarged version of TEX is available. I added a number of embellishments. 2. so gnuplot would output L TEX code. i. This includes.tex" command). a L TEX driver derived from A X should read the next . the one in GnuTEX. Gnuplot continues to print error messages to the terminal (stderr). gnuplot plotcommands.duke. Multiple filenames may be supplied to gnuplot this way. set terminal latex set output "eg1. Whenever hard copy is desired from gnuplot.) The terminal type fig outputs FIG code that can be read by the Fig graphics A program and translated into forms usable in both TEX and L TEX documents. intended for use with the EEPIC macro package for L TEX.0. labeled the tic marks for us. keep your plots simple and add \clearpage to your document where necessary. known as GnuTEX. 2 A Using gnuplot for LTEX: a Tutorial A Gnuplot is by nature an interactive program. Otherwise. The plot could then be A X document. Users making plots for L TEX will generally not use gnuplot interactively. The input file defines the output to be in L TEX. (A shell script (lasergnu) is supplied with the distribution that will accept a gnuplot command or input file and send the output to an Imagen or Postscript laser printer. supported only by the latex terminal. The gnuplot input file is given A below. the output file has to be closed by set output. This script may have been adapted to your site. output from the terminal type unixplot may be sent to a program interpreting the Unix plotting standard. In following examples.e. that A supported a new “terminal type” called latex. read in the order they are In 1988 and 1989 I created an alternate version. a tutorial.A Gnuplot L TEX Tutorial Version 4. A The reader should note that the L TEX picture environments output by gnuplot can be quite large and complicated. A There is also an EEPIC driver (eepic). then sent to the appropriate output device. and added a key in the upper-right-hand corner (this may be moved with the set key command.14] sin(x) Note that gnuplot has drawn in the axes. I will enclose the figure in a box to make it look a little better. producing a plot for this document. included in a L TE allowing the user to produce publication-quality In this example. runs through L TE Section 3 for more information. Usually. See EEPIC allows for much more efficient line-drawing. it is wise to use it. In late 1989 and early 1990 GnuTEX and a number of other gnuplot variants were merged together into A a new release of gnuplot. A There is a small package of auxiliary files (makefiles and scripts) that I find useful for making L TEX plots with gnuplot. π]. and the output appears as Figure 1. Example 1: Here is a first example. however.14:3. The terminal types imagen and postscript may be used for output to printers understanding those languages. without the file name specification. To produce the figure. and removed with unset key1 ). Anyone interested in using gnuplot with L TE and the primary gnuplot manual. the syntax set noXXX changed to unset XXX. and uses less memory.e. among many other improvements. In this case the output is directed to a file or pipe. scaled the y axis automatically. The output (one or more plots) may be piped to another program or redirected to a file. and can easily exceed the memory capacity of TEX. We now ignore the interactive nature of gnuplot and provide the input to gnuplot from a file. This is available for ftp as pub/gnuplot-latex.

5cm set output "eg2.4 0.2 0 0 1 2 3 4 5 This is the x axis 6 sin(x) Figure 2: A fancier example. Because of the limited picture capabilities of L TEX. including space for the labels.28] [0:1] sin(x) This is a plot of y = sin(x) 1 0. This may overload the memory of many TEX implementations.6 -0. In the first example. This is the size of the area used by the plot. command.4 0. Example 2: Now we will embellish the plot a little with some labels.8 -1 -3 -2 -1 0 1 2 3 Figure 1: A first example: y = sin(x). A A This is the default line style for the L TEX driver. set terminal latex size 7cm.A Gnuplot L TEX Tutorial Version 4.8 0..6 0.6 the y axis0.4 -0. this size was the default 5 inches by 3 inches. This input file produces Figure 2.4 2 1 sin(x) 0.tex" set format xy "$%g$" set title ’This is a plot of $y=\sin(x)$’ set xlabel ’This is the $x$ axis’ set ylabel ’This is\\the\\$y$ axis’ plot [0:6. many dots are required to approximate drawing a solid line. We have specified the plot to be 7 cm wide and 5 cm tall with the set term latex size .8 This is 0. .2 -0..2 0 -0. There are other line types available that draw dotted lines and use much less memory. The EEPIC driver draws solid lines with much less memory usage.

In this case. Note that they are A processed by L TEX and so may have math mode and other symbols in them. so generally each plot has its own input file and is kept in a separate output (. each plot command generates a new plot. Any valid floating-point A formatting specification. It is not likely that L TEX users will want this to happen.dat” × 5 10 15 20 Figure 3: An example with two curves on the same plot. unless specified by the user. The default is set format xy "%g". The ylabel can be moved around with optional offset parameters (see set ylabel in the gnuplot manual). and is shown here to illustrate the plot style option. but (of course) any A L TEX code in the string will not be interpreted. . These labels are also supported by the other terminal types. This plot is shown in Figure 3. a new page is produced. set terminal latex set output "eg3. This makes the labels look a little better. a style like lines that also plots a symbol at each data point. Here you will see that the x range was not specified.tex) file for inclusion at different places in the document.4 3 A We have requested that the format used by the x. is allowed in the format. In our next example. If the output is to a screen.and y-axis tic mark labels be in L TEX math mode. the screen is cleared.dat is plotted with style linespoints. a new picture is started. In the A latex case.tex" set format xy "$%g$" set title "This is another plot" set xlabel "$x$ axis" set ylabel "$y$ axis" set key at 15. The function y = x is plotted “with lines”. We have also moved the key to a different position. on one plot.dat". If to a printer. Typically. So far we have plotted one curve. the default range of [−10 : 10] is used. The x range is determined automatically. The %g represents the general-purpose floating point formatting specification for the printf function in C. We have also defined the range of both x (now [0. "eg3. it is defined by the range of the data file "eg3. A title for the plot and labels for the axes were set up in the next three commands.-10 plot x with lines. we will plot both a function and a data file on the same plot. The data file eg3. delineated with \\. If no data files or x range are supplied. 2π]) and y (here [0. Example 3: To place more than one curve on a plot. they will be used for all subsequent plot commands until they are changed. The ylabel may have multiple lines. Once these labels are set up. the ylabel needs to be moved to the left to avoid interfering with the left-hand side of the plot.dat" with linespoints This is another plot 20 15 10 5 y axis 0 −5 ×××××××××××××××× ×× × × × × ×××× ××× −10 ××××××××××× −15 −20 −20 −15 −10 −5 0 x axis x ”eg3. In gnuplot. The function is plotted over the same range.A Gnuplot L TEX Tutorial Version 4. 1]). y = sin(x). or L TEX command. which is the default plot style for functions. use one plot statement and separate the description of each curve by a comma.

Replacing the above set xtics command with the following gives us Figure 5. with the set xtics and set ytics commands. to spread out this command for readability. and another called dots that plots a tiny dot for each data point. The lines and linespoints styles use a different line style for each curve on the plot (in this example the dots have different spacing). the errorbars style can draw error bars at each data point (see the gnuplot manual). as in the title. Example 4: In the above plots of sin(x).5 −1 −3.\ .A Gnuplot L TEX Tutorial Version 4. The position and labels of the tic labels may be specified by the user. so the backslash had to be escaped. (The exception: a backslash at the end of a line—even within single quotes—is used by gnuplot for line continuation. Since pi is a predefined variable in gnuplot.14 −2. it would make more sense to label the axis in units of π. Note that the y axis label was delimited by double quotes.36 3.tex" set format y "$%g$" set format x "$%. Another form of this command allows us to specify the label and position of each tic individually. The points and linespoints styles produce a different point symbol for each curve on the plot (for up to twelve symbols. set xtics (’$-\pi$’ -pi.14 Figure 4: An example of the set xtics command. see Figure 8 for a complete list).79 This is the x axis 1. We have also turned off the key. We also make use of the line continuation character. we can use it anywhere we may use an expression.57 2.\ ’$-\frac{\pi}{2}$’ -pi/2.57 −0.) With a little more work. The set xtics command here specifies that the tics on the x axis start at −π and increment by π/4. after which they are re-used. gnuplot passes the backslashes through with no changes.00 0. Since no end point is given.5 sin(x) 0 −0. the tics continue to the right edge.2f$" set title ’This is $\sin(x)$’ set xlabel "This is the $x$ axis" set ylabel "$\\sin(x)$" unset key set xtics -pi. Finally. The style impulses draws a perpendicular from each point to the x-axis.4 4 There is a style called points that only plots the symbols at data points. This is demonstrated by the following example. pi/4 plot [-pi:pi] [-1:1] sin(x) This is sin(x) 1 0.36 −1. shown in Figure 4. Within single quotes. set terminal latex set output "eg4. the plot can look even better. and changed the format to restrict the x-axis tic labels to 2 decimal places.79 0. the backslash (\).

1 A Summary — Use with L TEX A In summary.\ ’$\frac{\pi}{2}$’ pi/2.1f\mu$’ set title "This is a title" set xlabel "This is the $x$ axis" set ylabel ’This is\\a longer\\version\\ of\\the $y$\\ axis’ offset -1 set label "Data" at -5.7 set key top left set xtic -10. You may find documentation for the various commands in the gnuplot manual. 3.5 −1 −π −π 2 0 This is the x axis π 2 π Figure 5: A fancy example of the set xtics command. A Going further: You should now be able to make a variety of plots for your L TEX document.5 sin(x) 0 −0. set terminal latex size 5.-5 right set arrow from -5. the first command to gnuplot should be set terminal latex and the output of gnuplot should be directed to a file.\ 3*exp(-x*x)+1 title ’$3e^{-x^{2}}+1$’ with lines lt 4 Line and point types: For reference.dat" title "Data File" with linespoints lt 1 pt 7. set output "plot.\ ’$\pi$’ pi) This is sin(x) 1 0. to use the most basic L TEX mode of gnuplot.tex" .-5 to -3. though hopefully this example is somewhat self-explanatory. for example.5.3.A Gnuplot L TEX Tutorial Version 4. we show all of the line and point types available in Figure 8. This is shown in Figure 6. 2.-6.4 5 "0" 0.tex" set format y "$%g$" set format x ’$%5.0.10 plot [-10:10] [-10:10] "eg3.0 set output "eg6. We will present a final example without explanation that showcases some of the capabilities of gnuplot.

tex file will be smaller. . the file will contain all of the plots you have specified (you probably only want one plot per file).A Gnuplot L TEX Tutorial Version 4. The quality of the output is about the same.0µ 10. To use EEPIC.4 6 This is a title 10 Data2 File 3e−x + 1 5 This is a longer version 0 of the y axis −5 • • −10 −10. and set clip.tex in your document as before. and use gnuplot as before.0µ 0. 5cm Then you do the (s)plot. and the dvi file will be smaller. then you can save L TEX memory. set key.. 3 Use with EEPIC A EEPIC is a macro package extending the picture environment of L TEX. L TEX will run much faster (and need much less memory). along with the document style options [epic. If you have the EPIC or EEPIC A macros. set gnuplot’s terminal type to eepic instead of latex. of course. The line styles will change. If you change the source. Include the file plot.0µ This is the x axis 5. the plot.tex extension. and your dvi translator supports the tpic \specials.eepic].0µ • • • • • • • • • • • • • • Data • • j • • • • −5.g. e. You will also want to read about the following commands: set title. \begin {figure} \begin{center} \input{plot} \end{center} \end {figure} This puts the plot into a figure. With A EEPIC pictures. and finally issue commands to close the file and switch the terminal back to the default by set output set terminal pop Finally. you can generate some more interesting line styles. The default plot size is 5 inches by 3 inches. A This file can then be used in a L TEX document. set label. set xtics.0µ Figure 6: An example of many features. These are all described in the regular gnuplot manual. set ylabel. set ytics. You can change this be specifying a size in either cm or inches when you select the terminal set terminal latex size 7cm. This may be anything you like but it should have a . set xlabel.

net is where you can send your e-mail.y.15.. or put the entire label in a \parbox. one for the eps A A part of the figure and one for the L TEX part. the labels in Figure 7 were generated with the following commands: x=.gnuplot.0 A In addition to the latex terminal..65. y=y-dy x. y=. or you can use gnuplot newsgroup comp. For example. The name of the L TEX file is taken from the ‘set output‘ command.A Gnuplot L TEX Tutorial Version u+\fr\ x. The plot can use either color or grayscale. . With the epslatex terminal you cannot use “\\” in a plain string to denote newline. The gnuplot documentation contains more information about the options and usage for each of these. y=.apps.05 set label "left torus:" set label ’$x=\cos u+\frac{1}{2}\cos u \cos v$’ set label ’$y=\sin u+\frac{1}{2}\sin u \cos v$’ set label "$z=\\frac{1}{2}\\sin v$" x=.} command. See the example below. 5 A What is new for TEX and L TEX terminals in gnuplot 4. You can either put each line in a separate label and specify vertical spacing manually.5in}{right torus:\\$x=1+\cos ac{1}{2}\cos u \cos v$\\$y=\frac{1}{2}\sin v$\\\ $z=\sin u + \frac{1}{2}\sin u \cos v$}’ at screen at at at at screen screen screen screen x.gnuplot..tex”) with “. dy=.eps” instead. 6 Sample of epslatex terminal driver The epslatex terminal driver allows you to mix the best features of TEX and PostScript. • mf and mp: Produces metafont and metapost outputs. Text elements are typeset by TEX while the graphic elements are created and positioned in parallel by gnuplot’s PostScript driver.08 set label ’\parbox{2.y left 7 Contact for help For general gnuplot questions. A • pstex and pslatex: Combined TEX / L TEX and postscript parts for text and lines.4 • pslatex and epslatex: Rewritten version. The resulting . with the postscript part included by \includegraphics{. Use of the terminal requires a lua interpreter that is called by gnuplot. the gnuplot mailing list info-gnuplot@lists. A • tikz: Much improved graphics compaired to earlier L TEXterminals.sourceforge. y=y-dy x.} command.tex file can be processed using pdflatex.. • emtex: Like the latex terminal. Addional sources of information are available on gnuplot homepage www. the following L TEX-friendly terminals are 7 4 A What is new for TEX and L TEX terminals in gnuplot 4. A • epslatex: Combined L TEX and postscript parts for text and lines. y=y-dy x. The name of the eps file is derived by replacing the file extension (normally “. respectively. but supports emtex specials: any line slopes contrary to a very limited A set of L TEX slopes. The driver produces two different files. included by \special{psfile=. respectively.

drawn using the epslatex driver.A Gnuplot L TEX Tutorial Version 4.5 1 0.5 0 -0.5 left torus: x = cos u + 1 cos u cos v 2 y = sin u + 1 sin u cos v 2 z = 1 sin v 2 right torus: x = 1 + cos u + 1 cos u cos v 2 y = 1 sin v 2 1 z = sin u + 2 sin u cos v Figure 7: Interlocking tori demo. .5 -1 -1. -1 0 1 2 left justified 3 centre+d text 4 right justified 5 6 7 test of character width: 8 rotated rotated -45 deg rotated ce+ntred text byby +45 deg 12345678901234567890 9 10 11 T 12 d    13   linewidth 14 d ' d  E lw 6 15 d   lw 5 pattern fill 16 d lw 4   ‚ d 0 1 2 3 4 5 6 7 8 9 17 lw 3 c 18 lw 2 lw 1 show ticscale No filled polygons latex terminal test + × ∗ 2 ◦ • ♦ ♥ ♠ + × ∗ Figure 8: The line and point types available depend on the exact terminal type.PM3D surface with depth sorting 1.4 8 Interlocking Tori .