concept subprocess in category gnuplot

appears as: subprocess
Gnuplot in Action

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

Table 4.3. Pseudofiles

Pseudofile

Description

"" (empty filename) Reuses the most recently encountered filename.
"-" Reads from standard input. In an interactive session, reads from the command window. (Terminate input with the EOF character or e.)
"+" Generates samples equally spaced x values covering the entire current plot range.
"++" Generates spaced x and y values covering the entire current plot range. (This is the two-dimensional equivalent to "+".) The number of points in the x direction is controlled by set samples, and the number of points in the y direction is controlled by set isosamples.
"< name" Reads from a subprocess via pipe.[a]
"| name" Writes to a subprocess via pipe.a
$heredoc Reads from a heredoc (no quotes around the name).

4.5.3. Reading data from a subprocess

On platforms that support input/output redirection, gnuplot can read from a subprocess via a pipe. Let’s say that a.out is a program that prints data suitable for gnuplot to standard output. The following command runs a.out and captures and plots its output without accessing the file system:

Gnuplot in Action: Understanding Data with Graphs

This is an excerpt from Manning's book Gnuplot in Action: Understanding Data with Graphs.

Output of the subprocess is always directed to the interactive terminal.

Perl allows us to open a subprocess as a file handle—in the same way we’d open a file—provided the first character in the filename is a pipe symbol (|). We can then write anything to this file handle using print. In the first line of listing 12.2, we open gnuplot as a subprocess and assign it to the file handle PROC. We then proceed to write some gnuplot options to this file handle, including the essential terminal information and output filename, followed by the plot command. The plot command is told to read from the special file dash (-), which means that it expects the data to follow inline. The data stream is terminated by the single letter e, and then the file handle is closed, which terminates the gnuplot subprocess. If we forget to close the file handle explicitly, Perl won’t terminate while gnuplot is still running!

Listing 12.3. Calling gnuplot from Python—note how we send two data sets to plot
import os import math gp = os.popen( '/usr/bin/gnuplot', 'w' ) gp.write( "set output 'graph.png'; set terminal png;" ) gp.write( "plot '-' u 1:2 w lines, '-' u 1:2 w lines\n" ) x = -5.0 while( x <= 5.0 ): gp.write( "%f %f\n" % ( x, math.sin(x) ) ) x += 0.5 gp.write( "e\n" ) x = -5.0; while( x <= 5.0 ): gp.write( "%f %f\n" % ( x, math.cos(x) ) ) x += 0.5 gp.write( "e\n" ) gp.close()

In Python, we can use the popen() function in the os module to obtain a file-like object that represents the gnuplot subprocess. Make sure to open this object in write mode, by passing a 'w' as second argument to popen(). (Alternatively, we could’ve used the subprocess module, which is new in Python 2.4.)

12.2.2. Capturing the output of a subprocess

Besides the system command, there is also a system( "..." ) function. The latter takes a command as argument, executes it as a subprocess, and returns its output as a string (in contrast to the system command, which always directs any output from the subprocess to the interactive terminal). For example, if we wanted to capture the current date and time information in astring variable, we can use the system() function and the Unix date utility: