6.3 Tabulating Functions and Slicing Data Files

PyXPlot’s tabulate command can be used to produce a text file containing the values of a function at a set of points. For example, the following would produce a data file called sine.dat containing a list of values of the sine function:

set output 'sine.dat'
tabulate [-pi:pi] sin(x)

Multiple functions may be tabulated into the same file, either by using the using modifier:

tabulate [0:2*pi] sin(x):cos(x):tan(x) u 1:2:3:4

or by placing them in a comma-separated list, as in the plot command:

tabulate [0:2*pi] sin(x), cos(x), tan(x)

The samples setting can be used to control the number of points that are inserted into the data file:

set samples 200

If the $x$-axis is set to be logarithmic then the points at which the functions are evaluated are spaced logarithmically.

The tabulate command can also be used to select portions of data files for output into a new file. For example, the following would write out the third, sixth and ninth columns of the datafile input.dat, but only when the arcsine of the value in the fourth column is positive:

set output 'filtered.dat'
tabulate 'input.dat' u 3:6:9 select (asin($4)>0)

The select, using and every modifiers operate in the same manner as with the plot command.

The format used in each column of the output file is chosen automatically with integers and small numbers treated intelligently to produce output which preserves accuracy, but is also easily human-readable. If desired, however, a format statement may be specified using the with format specifier. The syntax for this is similar to that expected by the Python string substitution operator (%)1. For example, to tabulate the values of $x^2$ to very many significant figures one could use:

tabulate x**2 with format "%27.20e"

If there are not enough columns present in the supplied format statement it will be repeated in a cyclic fashion; e.g. in the example above the single supplied format is used for both columns.

Footnotes

  1. Note that this operator can also be used within PyXPlot; see Section 2.3 for details.