Gnuplot tips

I like gnuplot – it’s great for a quick visualisation of some data or for scripting a large number of quick and dirty plots.  I suspect that I’m not alone in that (a) I wish that I had more facility with it and (b) partly because of (a), I tend towards more pointy-clicky tools such as OpenOffice for publication-quality plots.

Anyway, this site has some great gnuplot tips.  My favourite – how do you label data points using gnuplot?  You require a separate file of labels (which I’d say was con not a pro), but a Perl one-liner can easily do this for you.  Assuming that your data matrix, “data.dat” looks like this:

  A B C D
a 1 2 3 4
b 5 6 7 8
c 1 2 3 4
d 5 6 7 8

and you want to plot columns 2:3 – that is (1,2), (5,6) and so on, using column 1 (a, b, c, d) as labels then:

perl -ane ‘print “set label \”$F[0]\” at $F[1],$F[2]\n”‘ data.dat > label.plt

does the trick.  Edit out the first line of label.plt, then in gnuplot:

load “label.plt”
plot “data.dat” using 2:3

This will centre the data labels on each point so you may need to fiddle with the exact positioning.