xmg.pl hybrid perl/xmgrace language for generating plots from a script.
I like gnuplot, since it can be driven by simple scripts. However, the plots are not as professional as xmgrace, and the scripts rather simple compared to say what you can do with Perl. xmg.pl provides the solution to both problems, as it creates a hybrid language between xmgrace batch commands and perl, and feeds these to xmgrace through a named pipe.
#!/bin/sh
eval 'exec perl /home/zqex/bin/xmg.pl $0 ${1+"$@"}'
:
This makes sure any commandline arguments to the script are available in the @ARGV array. You to should change /home/zqex/bin to the location of the script. (remember to "chmod +x xmg.pl", and check if #!/usr/bin/perl is correct for your installation)
#!/bin/sh
eval 'exec perl /home/zqex/bin/xmg.pl $0 ${1+"$@"}'
#xaxis label "t"
#xaxis label "r\S2\N(t)"
#read xy "data"
#s_ symbol 1
#s_ line type 0
#s_ symbol color "red"
#read xy "theory"
#s_ symbol 0
#s_ line type 1
#s_ line color "black"
All lines starting with '#' are xmgrace batch language. s_ is a shorthand form to refer to the last read dataset. Look at the xmgrace manual for more information. An easy way to find all possible commands, is to save a project in xmgrace, and look at the file. The script loads one file 'data' using red circles, and another 'theory' as a black line.
#!/bin/sh
eval 'exec perl /home/zqex/bin/xmg.pl $0 ${1+"$@"}'
#xaxis label "t"
#xaxis label "r\S2\N(t)"
$sym=1;
$yshift=1;
foreach (sort glob "data/*")
{
#read xy "$_"
#s_.y=s_.y+$yshift
#s_ legend "$_"
#s_ symbol $sym
#s_ line type 0
$sym++;
$yshift++;
}
The glob finds all files in the data directory sorted alphabetically, and loads them with different symbol types, and sets the legend for each file. Each file is shifted +1 along the yaxis relative to the last file.
Is is often sensible to define perl functions that takes the filename and handles loading the file, a practical way to set symbols and colours is by evaluating a regex on the filename.
#!/bin/sh
eval 'exec perl /home/zqex/bin/xmg.pl $0 ${1+"$@"}'
sub AddTheory
{
$data=shift;
$qmin=1;
$qmax=10000;
for ($i=0;$i<$N;$i++)
{
##log distributed datapoints
$x=10**($i/$N*log10($qmax/$qmin)+log10($qmin));
$y=10^($x*$data);
#g$graph.s$set point $x,$y
}
#g$graph.s$set symbol 0
#g$graph.s$set line type 1
#g$graph.s$set line linestyle $ls
#g$graph.s$set line linewidth $lw
#g$graph.s$set line color 1
$set++;
}
$graph=0; $set=0;
##line style
$ls=2;
##linewidth
$lw=3;
#add theory lines to graph 0
AddTheory(1);
AddTheory(2);
AddTheory(3);
AddTheory(4);
Notice graph/set has to be handled manually. Secondly that all lines with '##' are regarded as comments.
#!/bin/sh
eval 'exec perl /home/zqex/bin/xmg.pl $0 ${1+"$@"}'
##xmgrace -geometry 1320x960 -autoscale none
:
'##xmgrace' is a special remark, that tells xmg.pl that the rest of the line should be used as argument to the xmgrace instance started by xmg.pl.
When running a script, what happens is that xmg.pl is called with the script as first argument, followed by any arguments on the commandline.
xmg.pl loads the script line by line. Lines with '#' are converted into print statements, and the result is a valid perl program. That program is the evaluated, and the output is send through a named pipe to xmgrace.
xmg.plmyscript code <args>
will print the pure perl code generated by processing myscript.
xmg.plmyscript eval <args>
will print the output from evaluating the perl program obtained from myscript.