The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

Creating nice figures using gnuplot

Gnuplot is very good at making figures. In addition to being able to do pretty much everything, it can do it from a script. Using this, it is easy to adjust the plot to match your needs.

Basic usage

When running gnuplot from a terminal, it allows you to enter commands and see the result. This is useful for testing features, inspecting some data, or to access the built-in help system (which is very good).

The main command is plot. This command generates a new plot with all the current settings. Changing any settings after the plot command will only affect new plots; already generated plots will remain unchanged. So you must always set everything up first and use plot as the final command.

The plot command can take many options, but you don't need most of them. The simplest form is

 plot 'datafile'

This will plot the numbers from datafile (text, containing whitespace-separated columns of numbers), which can be any path (relative paths are taken from wherever you started the gnuplot command). Without other options, it will use the first column for the data on the X axis, and the second column for the Y axis. If there is only one column, it will be the Y axis and the X axis will simply be 0, 1, 2, etc.

If you want to plot multiple curves in one figure, you can specify them in the command, separated by commas.

Each curve can have two modifiers. The first is what to plot. You can add using a:b to use other columns. For example, using 3:1 will put the third column on the X axis and the first one on the Y axis. You can also do computations on them: using 1:(($2-$3)/($4-$3)) can be used to correct for reference and dark in optical measurement, for example. Note that a computation needs to be within parentheses, and the columns are referenced with a $ sign (in ($2-1), the 1 is a literal number 1, while the $2 is the data from column 2).

The second modifier is how to plot. There are many options, which you can view using help plot from the gnuplot prompt. The most interesting one is using, which can be followed by the word lines, points, or linespoints. Other options set the size and linewidth of the markers and lines and a lot of other things.

One more option to note is title. It must be followed by a string (enclosed in single or double quotes) which defines what the data should be called in the key. If this option is absent, the name of the datafile and the using-declaration (if any) are used as the curve title.

The scale for the plot is automatically adjusted to fit all data. If that is not what you want, you can put ranges before the data:

 plot [-10:100] [30:35] 'data'

This will set both the X and Y range. If only one is specified, it is the X range. To specify only the Y range and let the X range be automatically determined, use an empty X range ([]).

Instead of data files, you can also plot functions. For example:

 plot [-10:10] 20*x**2+x-2, 'data'

will plot the function and the data. Functions cannot have a using or with option, but they can have some other modifiers. When plotting only functions, there is no automatic scaling, so you will need to specify a range.

Settings

There are many settings which can be used to customize plots. A list of all of them with an explanation what they do is available in the built-in help. A few are listed here.

There are two types of options. Boolean options, which can be on or off, are set using

 set key
 set nokey

This example sets and unsets the key option, which defines whether a key should be displayed. Similarly, there are grid and nogrid options to define whether a grid should be drawn.

The second type are options with a value. Those are written in the same way, but must be followed by a value. For example:

 set title 'Awesome results'
 set xlabel 'Horizontal axis (A.U.)'
 set ylabel 'Awesomeness (J)'
 set terminal postscript monochrome size 18cm,13.5cm
 set output 'plot.eps'
 plot [0:1] log(x) title 'Natural logarithm'

The first three lines set up some labels. The next two change the output; set terminal defines where the plot is going to be written; by default it is written to the screen, but with this command an eps file (black and white of given dimensions) is created instead. The last line specifies the filename for the output. Finally, the last line actually plots something, so with that command the file plot.eps is created.

Scripting

You don't usually want to use set terminal on the prompt. The reason is that you will need to redo everything you did in the session to produce a changed version of the plot. Instead, you will want to write all your commands into a text file and let gnuplot handle that. That's all there is to it: you just create a text file (using gedit for example; LibreOffice and Word do not create text files) and type the commands in there, just like you would have typed them at gnuplot's prompt. For example, you can use the commands from the example above. If you save the file as commands.txt, you can run all these commands from the commandline (the shell, not gnuplot) with the command:

 gnuplot < commands.txt

It will create the plot and return to the shell (that is, it will not give you a gnuplot prompt). You can see that the eps-file has been created. If it isn't exactly the way you want it, you can adjust the commands and recreate the file. Beware that it doesn't warn about overwriting existing files.

LaTeX

For those who want perfection, this isn't enough. You want to use formulas with greek symbols and fractions in the labels, those formulas must look just like formulas in the text of your document and you want the font to be identical to what you use in your text, to name a few requirements. All this is possible if you write your document in LaTeX (which you should be using anyway, if you care about such details). How to use LaTeX is not explained here, but a short recipe is given for how to include a plot in it.

It is possible to use the above procedure to generate an eps file and use \includegraphics{plot.eps}, just like you would include any other eps. However, that lets gnuplot do all the markup, so it may be different from what LaTeX would have done. A better way is to use the terminal type pslatex instead of postscript. (All the options after it can still be used.) The pslatex terminal will generate a plot.tex file, which can be compiled by LaTeX. Any labels are parsed by LaTeX, so formulas in them will be properly formatted, and LaTeX chooses the font to use. Because this is a TeX file, you cannot use \includegraphics to include it. Instead, you should use \input plot. Note that you should omit the file extension, and there are no braces. Doing this will not create a new paragraph for it, so you normally want to do that yourself using an empty line before and after the \input command. (But if you want several figures side by side, you can simply add them without breaks).

FA info icon.svg Angle down icon.svg Page data
Authors Bas Wijnen
License CC-BY-SA-3.0
Language English (en)
Related 0 subpages, 0 pages link here
Impact 179 page views
Created October 12, 2013 by Bas Wijnen
Modified March 7, 2023 by Kathy Nativi
Cookies help us deliver our services. By using our services, you agree to our use of cookies.