Gnuplot Quickstart

siiky

2022/01/12

2022/07/14

en

I started learning Gnuplot for a PA because I didn't want to deal with Python BS or whatnot, and Gnuplot has been around since like... even before the dinosaurs were invented, so it must be super specialized for this kinda thing, and it must be pretty good right?

Hopefully I'll document well enough the things I've learned these past few days, for posterity or someone else. I've been using it to plot 2D graphs of recorded data, not functions/expressions, so my focus will be on that.

Gnuplot files don't have a "standard" file extension, but some common ones seem to be .gp, .plot, .gnu, .gnuplot, .plt. I've been using .gp and will use .gp here.

To run Gnuplot scripts, just call gnuplot script.gp. It's possible to pass arguments to the script by using the -c flag: gnuplot -c script.gp arg1 arg2 etc. And inside the script the arguments are available as the variables ARG1, ARG2, etc. As is common on other programming languages, ARG0 is the script name. I don't know if there are, or what are the limits on the number of arguments, nor how to loop through them, but I'm guessing it's possible.

Now let's get going with some Gnuplot code. I said the focus would be on plotting datafiles, so let's start with expressions:

exp.gp

exp.svg

Notice how it starts to grow really fucking quick after t=5 -- right after HTML was invented.

Here's another one:

rollercoaster.gp

rollercoaster.svg

The website, with documentation and all (including a 300+ pages PDF of all the documentation, with proper PDF index!):

http://www.gnuplot.info

http://gnuplot.sourceforge.net

The first seems to be the "official" one, but is sometimes offline? The second looks like a mirror.

You can use the help command to read the documentation inside the Gnuplot REPL too.

An important concept is that of the terminal, as seen above being set to SVG. It's nothing but an "output backend", and Gnuplot has tons of those -- run set terminal and see for yourself; there's even one to output ASCII art to the terminal! Different terminals may have different specific options -- RTFM for those.

Once you start messing around with line styles, line types, colors, and whatnot, it's helpful to know what the valid values are. For that use the test command after setting the terminal (the result of the test command varies depending on the terminal, so it's important to set it):

gnuplot-test.gp

gnuplot-test.svg

Variables are a thing, and you can define them just as you'd expect:

To plot data from files just pass the filename to plot:

Gnuplot is supposed to support many different formats but I don't know details here. I've been using TSV because it makes sense. For tabular data files (TSV, CSV, ...), this may be useful:

RTFM for details: help set datafile separator.

It's possible to define datasets inside a Gnuplot script, too, like this:

Notice the e at the end! You can even define more than one for the same plot command:

Another arguably more useful way is to do it like so (notice the dollar!):

This kind of inline data definition doesn't seem to work on the REPL though... At least I couldn't make it work.

For tabular data files, files may have many columns, some that you want, some that you don't, some that are in the wrong order... To solve that, you use using:

The above uses the first and third columns of the dataset.

And with that, if you want to plot several graphs from the same dataset, you can do it like so:

Assuming the data file has at least 4 columns, the above will plot a line/w.e. using the first and third columns, and then another using the first and fourth columns. The empty string there is a shortcut to mean "the previous dataset/file".

For certain plot types, such as for errorlines or errorbars, you may want or need to use more than 2 columns of data.

And a final plot, pretty much the most advanced I can get right now. The dataset's fields are separated by tabs but your browser or something may present them as spaces, so download the file for greater €€€profit€€€.

errorlines.gp

errorlines.svg

-----

Just a couple of notes on security, especially for someone wanting to develop an interface library. These are things that may be useful when writing and running scripts directly in Gnuplot, but that are a security nightmare if left as something to think about tomorrow.

system() a la C is a thing!

And so are backticks like in shell languages! The first line of the following Gnuplot code runs the echo command, but the second one doesn't: