Very simple-minded automation of gnuplot

I have lots of datafiles I want to plot. gnuplot is scriptable. Since I don’t need any fancy output yet — this is data investigation, not manufacture of publication-quality diagrams — I can get multiple plots out quickly by writing a simple (simplish) couple of scripts.

I am sure people who know bash and perl and stuff can do this much better, but this works for me.

I have a wrapper script that simply consists of multiple calls to an inner script. It is here that I select the files I want to plot:

$ cat
./ r_0001_o_xy_TPA_21_Biso4p8_reread.inp
./ r_0001_o_yz_TPA_21_Biso4p8_reread.inp
./ r_0001_o_zx_TPA_21_Biso4p8_reread.inp
./ r_0050_o_yz_TPA_21_Biso4p8_reread.inp
./ r_0050_o_zx_TPA_21_Biso4p8_reread.inp

I know I could do this in a single script, but I like to be able to call the inner one directly if I just want to make one plot. The inner script, looks like this:

$ cat
epsname=`basename $1 .inp`.eps
echo $epsname
scriptname=`basename $1 .inp`.gp
echo $scriptname
echo "set term postscript eps solid enhanced font 'Times,24'" > $scriptname
echo $epsname > temp1111
echo 'set output "' > temp2222
echo '"' > temp3333
paste -d '' temp2222 temp1111 temp3333 >> $scriptname
echo "set angles degrees; set polar" >> $scriptname
echo "set size square" >> $scriptname
echo "set xrange [-0.8:0.8]" >> $scriptname
echo "set yrange [-0.8:0.8]" >> $scriptname
echo $1 > temp1111
echo 'plot "' > temp2222
echo '" w l lw 3' > temp3333
paste -d '' temp2222 temp1111 temp3333 >> $scriptname
echo "set output" >> $scriptname
echo "set terminal x11" >> $scriptname
echo "quit" >> $scriptname
cat $scriptname
rm temp1111 temp2222 temp3333
gnuplot $scriptname
ls -ltrh $epsname

What’s going on here?

epsname=`basename $1 .inp`.eps
echo $epsname
scriptname=`basename $1 .inp`.gp
echo $scriptname

The bit above just uses basename to create the two filenames I need, one for the encapsulated postscript output, and one for the gnuplot script. Then I basically assemble the lines I want to see go into the gnuplot script. Mostly I can just echo stuff into the file, but I have various quote marks within quote marks, and (for simple-minded me) the solution that I know works is to go via the paste command, and write stuff out to little text files. I am sure there are much tidier ways to do this, but this works for me.

The line:

paste -d '' temp2222 temp1111 temp3333 >> $scriptname

causes the contents of the temporary files to be pasted together and put into the script, where the delimiter (‘-d‘) is the thing between the single quotes — which is nothing since the quotes are adjacent. This pastes stuff together with no gaps. The things being echoed into the file are gnuplot commands of various kinds.

Then I cat the script to the screen so I can see what it is doing (if I direct the output from the plotting script into a file, this lets me capture the commands I used), then I remove the temporary files, run gnuplot, and list the newly made file.

Clunky, but simple and effective. Useful changes could include outputting to pdf or some other format, and adding a step at the end (perhaps using pdfjoin) to combine all the resulting files in a single file.

Here is a png of the result. The gnuplot formatting could be made much nicer (title is too big, etc), but the overall process can always be improved, so I’m not worried about that. And the fact that it is a script means I can replot quickly when I know what the final format should be. For now I just want to see what I’ve got.

A simple plot from a diffuse scattering simulation, made using gnuplot.

A simple plot from a diffuse scattering simulation, made using gnuplot.

Whatever suits.


Tags: , , , , ,

About Darren

I'm a scientist by training, based in Australia.

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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: