One of the strengths of gnuplot is that it can be used to automatically
generate plots en masse (presumably with some sort of script). Although
Windows' support for script processing is not quite as convenient as it is on
*nix operating systems, most things can be accomplished with a bit of effort.
For instance, rather than issue commands purely within gnuplot's command
window, I nearly always write the commands in a gnuplot script file and
load
that instead.
Although it's easier to develop the first version of the plot/script by
trying commands out interactively, one often wants to generate a permanent
copy of the plot, and sometimes to do this without interacting with the
program. And thereafter, it's often handy to be able to work with a script in
either batch mode, or interactively. Adding the following instructions to a
script adapts it to whichever mode it's in. It defaults to batch mode because
if I'm actually using it interactively, it's easy for me to tell it so (with
the command interactive=1
).
unset multiplot
if ( exists("interactive")==1 ) {
print "Interactive mode"
set terminal windows size 1024,576 font "Arial,14"
} else {
set terminal png size 1024,576 font "Arial,18"
# Use PDF if you want one plot per page (as opposed to a multiplot).
# 11.7 and 8.3 gives A4 landscape in inches.
# set terminal pdf size 11.7,8.3 font "Arial,18"
# Remove the file name extension
z=strlen(fn)-4
fn_base = fn[1:z]
# Remember to change the extension to .pdf for the PDF terminal
set output fn_base.'.png'
}
The first instruction disables multiplot mode off because gnuplot would
aborts the script error if the terminal is changed whilst multiplot mode is
enabled. The script assumes that the name of the file containing the data
is stored in the variable fn
. It generates the name of the plot
file by stripping off the extension, and applying a new one to reflect the
type of the output file. I use PNG for single plots, or a single 'page'
multiplot, but use PDF where I want each plot/multiplot to appear on separate
pages.
If you're generating more than one plot in interactive mode, you'll probably want a pause between them to give you a chance to view them. This can be achieved, without interrupting gnuplot when it is in batch mode, using the following code:
if ( exists("interactive")==1 ) {
pause -1
}
There is a slight limitation when interactively plotting to a file, which
is that Windows' Image Preview feature can't read a plot whilst it is still
open in gnuplot. To close the plot without actually closing gnuplot, just
use the command unset output
. Generally, though, I use
set terminal windows
or set terminal
wxt
terminals when I am using gnuplot
interactively.
By storing the commands for generating a plot in a script file, one can process, for example, all the files in a directory with a given extension. Here's a simple Windows batch statement that does just that:
for %%G in (*.tsv) do gnuplot.exe -e "fn='%%~nG'" myscript.plt
This will run the gnuplot command for every .TSV file in the current directory.
gnuplot can take plotting commands on the command line using the
-e
argument, and we can use this to apply a plotting script to
each file. The -e argument's outer value is surrounded by double quotes in
case the file name contains spaces. %%~n
is a feature of
Windows' batch language that generates a file name without the extension; the
extension can then be added to the data file and image file inside the
gnuplot script. If you want to run this command from the command line (as
opposed to within a batch file), remove one of the %
symbols
from each of the two pairs. Note that the batch command will require that
gnuplot.exe is present in your computer's PATH environment
variable.
Home | About Me | Copyright © Neil Carter |
Last updated: 2016-05-24