Gnuplot is a light-weight and easy to use scientific plotting tool. Its has enough functionalities to deal with most of my demands on visualizing experimental data. If heavy computation is needed, GNU Octave can be used, of which the plotting backend is still Gnuplot. Originally, when I had a set of data files with the same structure to be plotted, I would write a Gnuplot script which could process only one file. After verifying and adjusting the script to function correctly, I would make copies of this template for each of the data files and then correspondingly change the data file name in each copy. Although, this is not elegant, it actually does the work.
Today, I thought about if I could automate the above process by writing only one general script which could process the specific file prescribed in the command line option. Because a Gnuplot script run from the command line cannot be passed any arguments, I have to think other ways. Luckily, the command gnuplot has an option -e, with its function described as follows:
-e "command list" executes the requested commands before loading the next input file.
Therefore, variable definitions can be given in the above "command list", which can be accessed from within the Gnuplot script. Multiple commands can be listed which should be separated by semicolon. For example, the following command can be run from the terminal to pass data file‘s base name and another parameter into the script plot.gpl:
gnuplot -e "basename=‘2013-10-18 sample A‘; legend_str=‘RMS‘" plot.gpl
In plot.gpl, there can be something like this:
set output basename." voltage.eps"
plot basename." raw.dat" using 1:3 lt 1 lc rgb "red" lw 1 title legend_str with lines
set output
system "eps2png \"".basename." voltage.eps\""
In the above, the period symbol is used to concatenate strings. Once we can pass arguments into the Gnuplot script, we can further use bash script or other high level script languages to process all the data files in batch. For example, the one used for plotting dc conductivity data files is as follows, which utilizes bash programming and the powerful gawk.
#! /bin/bash
# Batch plot all the data files
for dat_file in *\ processed.dat; do
formulation=`cat "${dat_file}" | grep -i formulation | gawk ‘{gsub(/^.*: */, ""); print}‘`
dat_file=`echo "${dat_file}"|gawk ‘{print $1,$2,$3}‘`
echo "Start processing ${dat_file} for ${formulation}"
gnuplot -e "basename=‘${dat_file}‘; formulation=‘${formulation}‘" plot_single_data.gpl
echo "Finished processing ${dat_file} for ${formulation}"
done
Pass command line arguments to Gnuplot script