Cool code: plotting columns from many data files with Grace
Grace a.k.a. xmgrace is a really useful tool for plotting histograms from tabular data files. Its power comes from the command line control and being scriptable. Yes, there are other options which are sometimes more suitable for specific situations (e.g. GNUplot, Matplotlib/PyLab), but for quick, basic plotting I usually find myself relying on xmgrace.
Here is an example of a single line command to plot two columns from each of a large number of data files:
for i in ./a*/field.log; do echo -n " -block $i -bxy 10:44" ; done | xargs xmgrace
The command searches all current subdirectories with names beginning “a” for files called field.log. For each field.log file found it uses an echo command to generate a commandline argument string that we normally use to make a 2D plot with the 10th (x axis) and 44th (y axis) columns using xmgrace. The -n option for echo makes sure there is no newline after each echo, generating one long collection of commands. We next want to pass this on to xmgrace… xmgrace_ does not take input from _stdin_ so we first pipe the total echo output to _xargs_, which is a tool used to construct command line parameters from _stdin_. _xargs_ then passes these command line arguments to _xmgrace_.
This little code snippet saves a lot of typing, which grows of the order N for N data files. Now I need to work out how to name each data set on the fly too.