Pages: Welcome | Projects

Handy Gnuplot

2015/8/19
Tags: [ gnuplot ]

GNUPlot is a powerful application, and every day I use it I like it more. It's not exactly super-easy to use it, mainly because of the manual, which is sometimes complicated…

While using it, I found myself rewriting the same things again, so I followed the good suggestion of having snippets. Here I share a bunch of good snippet ideas.

My snippets:

const.gpi

A set of constants always loaded by my $HOME/.gnuplot file. Always useful:

second  = 1000
minute  = 60 * second
hour    = 60 * minute
day     = 24 * hour
month   = 30 * day
KiB     = 1024
MiB     = 1024**2
GiB     = 1024**3
KB      = 10**3
MB      = 10**6
GB      = 10**9

(Note: here my constant for second is 1000 because I tipically handle data in milliseconds).

dark.gpi

My screen is dark. I find it more relaxing, but GNUPlot sticks the white terminal just in my eyes, and gives me eyestrian every time I plot somethng.

I sought for a way of getting it dark here, and then I simply took the advices I got, with some slight modification.

set style line 101 lc rgb '#a0a0a0' lt 1 lw 1
set style line 102 lc rgb '#707070' lt 0 lw 1
set border 3 front ls 101
set grid ls 102
set terminal wxt background '#00222222'
set xlabel textcolor rgb '#808080'
set ylabel textcolor rgb '#808080'
set y2label textcolor rgb '#808080'
set title textcolor rgb '#808080'
set key textcolor rgb '#808080'

Time scale and minutes.gpi

I use quite often the x axis for time. In my $HOME/.gnuplot I keep two variable definition which give me a default scale for it:

tscale  = hour      # from constants
tlabel  = 'Time [hour]'

So when I plot something I usually do

set xlabel tlabel
plot ... using ($1 / tscale):2 ...

Then I've got a file named minutes.gpi, which I load whenever I want to change scale to minutes:

tscale = minute
tlabel = 'Time [minute]'

periodic.gpi

Finally, periodic.gpi. Nice when you are monitoring some ongoing situation, and you want some sort of telemetry.

while (1) {
    replot
    pause 1
}

(this will wait 1 second between each replot)

Combining them together

I've listed a bunch of generic snippets. Of course I also keep a number of very specific scripts for my needs, which will assume certain data layouts and file names. All of them are in a directory, pointed by the $GNUPLOT_LIB environment vairable.

Now, a nice thing of gnuplot is the -p (persistent) flag, which allows the plot window to survive the termination of the command. It's very fast and effective to run gnuplot as follows:

gnuplot -p foo.gpi

Where foo.gpi is the script for my specific plot.

The cool thing is combining scripts together. If I want to plot it on a dark window, for instance, I can do:

gnuplot -p dark.gpi foo.gpi

I want to have it scaled on minutes

gnuplot -p minutes.gpi dark.gpi foo.gpi

Or I want my process to survive indefinitely, while the window keeps me informed on the progress:

gnuplot minutes.gpi dark.gpi foo.gpi periodic.gpi

(here the -p is useless, the command will not terminate unless sig-termed or worse…)