Blog / Gnuplot

Wednesday 23 July 2008

Automating Gnuplot with scripting

I have recently found myself with a large number of data files that all wanted to be fed through Gnuplot to produce a number of figures. Not wanting to spend ages manually feeding them in to Gnuplot I decided to have a go at scripting something to save myself a lot of time. I hope that sharing this here is of use to somebody out there.

The script, script.sh:-

#! /bin/bash
ls *.data | sed "s/.data//" > list
for i in `cat list` ; do
   sed -e "s/INPUTFILE/$i/" -e "s/OUTPUTFILE/$i/" \
    plot.gnu | gnuplot
done
rm list

The script, written in Bash, creates a file called list in which all the data files matching the condition *.data are listed (stripped of their file extension .data). It then reads through this file invoking sed to replace INPUTFILE and OUTPUTFILE in the Gnuplot script plot.gnu (see below) with the name of the data file and passes this to Gnuplot. Finally, the temporary file list is removed. Simple!

Remember the script needs to be executable: chmod +x script.sh

The Gnuplot file, plot.gnu:-

set term postscript enhanced color
set output 'OUTPUTFILE.eps'
set title 'OUTPUTFILE'
p 'INPUTFILE.data' u 1:2 w l

Obviously you can make the Gnuplot file as advanced as you like to produce the sort of figures you desire with your data.

— Andy

Posted in:

Comment

Tuesday 08 July 2008

Uesful Gnuplot websites

Gnuplot is an incredibly useful free program for plotting functions and data that I use a lot. Some day I intend putting together a guide to making the most out of it, but in the meantime here are a collection of useful websites for using this surprisingly powerful piece of software.

The official Gnuplot website is the place to go to download a copy of Gnuplot as well as a useful resource for detailed documentation on what the program can do and how, including a number of examples. However, for newcomers the documentation can be a bit bewildering; a number of sites have good introductions to using Gnuplot, one of the better of them is the Math Blog’s Getting started with Gnuplot. Another good tutorial is this Duke University Gnuplot 4.0 tutorial page. IBM have a good article on visualising 2D data with Gnuplot.

If you’ve got to grips with the basics and want quick answers an excellent resource is the Gnuplot: Not so frequently asked questions website, an invaluable site that I’ve made a great deal of use of over the years (and still do).

If I find more I’ll add them. If you want to suggest a good Gnuplot site then please leave a comment below.

— Andy

Posted in:

Comment

Recent blog posts

Links