My software notes

May 12, 2007

shell script + gnuplot

Filed under: softwares and scripts — kpwu @ 3:36 pm

I have to analyze about 70 PFG-NMR data (maybe more..), it’s not fun when deal with each data point routinely. Here is the goal I have and the solution I did.

Goal: using shell script (awk, UNIX tools and sed) to do simple calculation; then use gnuplot to generate a lots of plots.


  1. use shell script to format correct files that gnuplot can take as inputs
  2. run gnuplot by calling run.gnu (a gnuplot script)
  3. look at the PNG file generated by gnuplot

Result: see the figure here:
dioxane diffusion plot
content of shell script:

rm -f fit.log
for INP in *k.dat
newname=`basename $INP .dat`
paste gzlvl6.dat $INP > _temp1
cat _temp1 |awk ‘{printf \
“%4.3f%10.3f%10.3f%12.3f\n”,($1*0.00235)^2,log($5/100),$1,$5 }’ >_temp2
## make a proper script to generate gnuplot with good title
cp run.gnu _temp.gnu
echo “plot f(x) with lines ls 1 title ‘fitting line’, ‘”_temp2″‘ using 1:2 title ‘dioxane $newname’ with points 6” >> _temp.gnu
/usr/bin/gnuplot _temp.gnu
## post-filename-modification
mv fit.log $newname-fit.log
mv test.png $newname.png
cat _temp2 |sort -n > $newname-all-info.txt
rm _temp*

Content of run.gnu:
set terminal png
set output “test.png”
fit f(x) “_temp2” using 1:2 via a,b
set xlabel “G^2”
set ylabel “ln(I/I0)”
set yrange [-4:0]
set style line 1 lt 1 lw 3



  1. I also did something like this when I was a research student. Sometimes I even used the script to put everything into a report in HTML format. It’s funny to see everything ready in a second.

    Do you use plotmtv? It’s good for plotting 3D surfaces.

    Comment by 有涯 — July 19, 2007 @ 2:59 pm | Reply

  2. Thanks for the information of “plotmtv”. It looks nice and easy to learn.
    Currently, I make 3D plots by the ProFit on my MacBook Pro. I don’t need to generate some 3D plots by gnuplot+scripts.

    Comment by kpwu — July 20, 2007 @ 2:11 pm | Reply

  3. In addition to gnuplot, xmgr (now called Grace) is also very nice for 2D. I like the feature of doing Fourier Transform, finite differences, etc. on the source data without requiring more programming.

    Just for your information.

    Comment by 有涯 — July 21, 2007 @ 10:58 am | Reply

  4. Yes, XMGR/Grace is a very good plot software. I had used it for several course reports last year. It works well by scripts and by its GUI. I didn’t successfully install it on my MacBookPro, therefore, I still use xmgr to do some data analysis by accessing to a remote Linux cluster.

    Comment by kpwu — July 21, 2007 @ 6:54 pm | Reply

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at

%d bloggers like this: