concept script in category gnuplot

appears as: scripts, script, A script, The script, The script
Gnuplot in Action

This is an excerpt from Manning's book Gnuplot in Action.

As long as you keep these three items in mind, creating files via terminals won’t pose any difficulties. (In chapter 11, you’ll see how to use scripts and macros to facilitate this process.)

The script itself is straightforward but messy, because much of the string handling has to be done explicitly. Moreover, gnuplot doesn’t follow the convention of most contemporary programming languages of using zero-offset strings and asymmetric bounds, thus necessitating the explicit conditional when splitting the argument into path and basename. (To reduce clutter, I have refrained from prefixing all internal variables to make them “private.”)

As I said, I regard this example as a “technology demonstrator” that shows how you can encapsulate complex tasks in scripts and use them like subroutines. You decide whether you think this is the right approach.

The approach presented in this section is different, because it relies on an intermediate layer, written in a general-purpose programming language (Python, in this case, but it could, of course, be anything). The script in listing 11.10 acts as a filter: it gathers data from standard input and passes it on to gnuplot only if new data is available. It therefore implements a “push” model, rather than the “pull” model of the previous solutions. The script also acts as a buffer, storing the most recent set of points and passing the points to gnuplot. This is necessary because now the file system is skipped entirely, and gnuplot itself doesn’t retain data between plots.

Listing 11.10. A data monitor in Python (file: monitor.py)

The script runs gnuplot as a subprocess and communicates with gnuplot by writing to gnuplot’s standard input via a pipe. The script reads incoming data from its own standard input (not from a file), splits and parses the intake, and pushes it onto a buffer, before passing it on to gnuplot. In this case, there is no need for gnuplot’s stats command, because the determination of the plot range is now done in the Python script.

The script is straightforward. Only two Python-specific implementation details require comment:

  • To achieve real-time behavior, all input and output must be unbuffered. This is the reason the script uses sys.stdin.readline() in a while loop, instead of the more familiar idiom for line in sys.stdin:. Also, make sure to run this script using Python’s -u command-line option.
  • Scripts provide a workaround. A script can contain any valid gnuplot code and is callable as a single unit. The generic export script is a good example (compare section 10.1.2):

    Be sure to assign the proper value to this variable before invoking the script (assuming that the script is called export.gp):

    outfile = "graph.pdf"
    load "export.gp"

    The alternative is to use the call command to invoke the script (instead of load). The call command accepts up to nine parameters on the command line, which are available in the script in variables called ARG1, ARG2, and so on. (The variable ARG0 is set to the name of the script file, and the variable ARGC holds the number of arguments supplied to call.) A version of the export script, suitable for call, looks like this:

    sitemap

    Unable to load book!

    The book could not be loaded.

    (try again in a couple of minutes)

    manning.com homepage
    test yourself with a liveTest