BatchPlot

2009.03.08

BatchPlot is a simple Unix shell script that uses gnuplot to plot all files in a specified directory. Naturally, gnuplot must be installed for the script to work.

A large number of data files can be tedious to plot manually. In the university engineering departments I've been in, MATLAB is often used to automate the process. However, the MATLAB software package is quite expensive and doesn't always produce the most visually pleasing results (in my opinion). It also does not save graphics in the eps vector image format, which is the default graphics format for the popular LaTeX typesetting system. For these reasons, I wrote a script that would automate gnuplot plotting for multiple files.

BatchPlot requires gnuplot and a Unix or Linux system that can run Bourne shell scripts.

To use, first download the archive below and extract it using the archiving tool of your choice, or cd to the directory and run:

tar xzvf batchplot0-1.tar.gz

The batchplot.sh file in the root directory contains the program.

Some example files are contained in the example folder. Try typing:

./batchplot.sh -d 'example/' -f 'SPE' -l ',' -n '0844' -s 10 -e 1010 -x 'nm' -y 'dBm'

This command plots all the files in directory 'example/' with file extension 'SPE' and with file names starting with '0844'. These files contain data organized with comma deliminators, and the data starts from line 10 and ends on line 1010 with an x-axis label value of 'nm' and a y-axis label value of 'dBm'. The full insctructions can be viewed by typing:

./batchplot.sh -h

View Source
#!/bin/sh
# BATCHPLOT - multiple file plotter script
# Ver. 0.1 (2009/01/28, test version)
# Copyright (c) 2010 Tianhe Yang [ http://tianheyang.com/ ]
# Licensed under the GNU General Public License [ http://tianheyang.com/documents/applications/license.txt ]
# Requires gnuplot

legend()
{
  cat << EOF
  legend: $0 options

  This script will plot all files in a directory with the specified file extension.

  OPTIONS:
  -h      Show this help document.
  -t      Type of plot. 'single' makes a new plot for each file, 'multi' plots all files on a single plot. The 'multi' option has not yet been implemented.
  -d      Directory of the files.
  -f      Extension of the data files to plot. Default value is 'txt'.
  -l      Delimiter symbol. Space: ' ' Comma: ',' Tab: '\t'
  -n      Name of the group. For example, if the files you wish to plot are named 'test01', 'test02', etc., the name of the group would be 'test'. Default behavior will plot all files with the specified file extension.
  -s      Some data files contain header information. Specify the line number to start reading from.
  -e      Some data files contain footer information. Specify the line number at which the data ends.
  -m      The output data type. Can be 'eps', 'pdf', or 'ps'.
  -x      x-axis label.
  -y      y-axis label.
EOF
}

TYPE="single"
DIR=""
EXT="txt"
DELIM=","
GROUP=""
START=""
END=""
OUT="eps"
OFFSET=0
XLABEL="Wavelength (nm)"
YLABEL="Power (dBm)"
while getopts “h:t:d:f:l:n:s:e:m:x:y:o” OPTION
  do
    case $OPTION in
    h)
    legend
    exit 1
    ;;
  t)
  TYPE=$OPTARG
  ;;
d)
DIR=$OPTARG
;;
f)
EXT=$OPTARG
;;
l)
DELIM=$OPTARG
;;
n)
GROUP=$OPTARG
;;
s)
START=$OPTARG
;;
e)
END=$OPTARG
;;
m)
OUT=$OPTARG
;;
x)
XLABEL=$OPTARG
;;
y)
YLABEL=$OPTARG
;;
o)
OFFSET=$OPTARG
;;
?)
legend
exit
;;
esac
done

bashplotsingle()
{
  LIST="$(ls $DIR$GROUP*.$EXT)"
  for file in $LIST; do
    if [ -e $file ]; then
      echo "Processing $file..."
      gnuplot << EOF
      set terminal postscript $OUT color enhanced
      set encoding default
      set size 1,1
      set key off
      set datafile separator "$DELIM"
      set grid
      set xlabel "$XLABEL"
      set ylabel "$YLABEL"
      set output "$file.$OUT"
      plot "$file" every ::$START::$END using 1:2 with lines lt -1
EOF
      echo "Done!"
      else
	echo "The file \"$file\" is missing."
    fi
done
}

bashplotmulti()
{
  echo "Multi plots have not yet been implemented."
}

bashplot$TYPE -d $DIR -n $GROUP -f $EXT -l $DELIM -s $START -e $END -m $OUT -x $XLABEL -y $YLABEL -o $OFFSET

exit

❶ download tarball

❷ report bug

❸ view terms

footer