An simple graph can help you visualize complex data and be a very useful tool in presentations and reports. The plotutils package [1] brings together a group of programs that all access the functions of GNU Libplot.
This library produces device-agnostic vector graphics and even animations in various output formats. There are bindings for C and C++ among other library classes. And, because of the effective interface programs for the shell, in most cases, you don't need to program anything – it's often enough to invoke a smartly crafted command line.
The plotutils package is found in the major distribution repositories so that it can be set up easily with the corresponding package manager. In Ubuntu, you can just use the apt-get install plotutils command with administrative rights. This step pulls in all the necessary dependencies.
You select the desired output format (Table 1) with the -T option as an argument to the command, such as graph -T X … , where plotutils accepts multiple instances of -T . You just need to supply a suitable output redirection:
$ graph -T X -T PNG ... > output.png
Table 1
Plotutils Formats
Identifier | Format |
---|---|
AI | Adobe Illustrator |
FIG | Editable vector graphics format for the Fig program |
GIF | A simple version of GIF |
Metafile | GNU device-independent graphic format used internally |
PNG | PNG files, useful for further editing |
PNM | Portable Anymap, a universal bitmap format |
PS | PostScript, convertible to PDF |
SVG | Classic scalable vector graphics format |
X | X window display format that creates no output file |
Several programs are used for graphics output. The main one is the graph program seen in the previous example. Using --help , you can display all the available options, which are surprisingly clear (see Table 2).
Table 2
Graph Options
Option | Function |
---|---|
-T <format>
|
Output format |
-f <size>
|
Font size in pixels |
-F <font>
|
Default font style |
-g <style>
|
Grid style (0 through 3 ) |
-h <height>
|
Fractional height of the plot |
-k <length>
|
Length of tick marks |
-K <clip_mode>
|
Activates the clip mode |
-r <shift>
|
Shifts the plot horizontally by a fractional amount |
-s
|
Saves the screen |
-u <shift>
|
Shifts the plot vertically by a fractional amount |
-w <width>
|
Fractional width of the plot |
--bitmap-size <X>x<Y>
|
Size of the graph display |
--max-line-length <length>
|
Amount of points per line |
--page-size <type>
|
Output page type, such as "a4" |
--pen-colors <colors>
|
Pen colors as a string, such as "1=red, 2=green" |
--rotation <degrees>
|
Rotation angle |
-E <axis>
|
Toggles the X and Y axis ends |
-l <axis>
|
Toggles the axis between linear and logarithmic |
-L <label>
|
Text string for the top label |
-N <axis>
|
Toggles the presence of tick marks on axis |
-x <limit>
|
Sets limits for the X axis |
-y <limit>
|
Sets limits for the Y axis |
-X <label>
|
Text string label for the X axis |
-Y <label>
|
Text string label for the Y axis |
The "plots" created with plotutils consist of the plotted curve, the X and Y axis with their labels, and other possible elements such as a plot title and background grid.
Graph accepts binary and plaintext files, with the latter being significantly easier to handle in that you can easily create and modify them in a text editor. You can use additional Graph options to interpret the input data (Table 3).
Table 3
More Graph Options
Option | Function |
---|---|
-I <format>
|
Defines format; a ASCII; e ASCII with error bars, g Gnuplot table format, f Single-precision binary, d Double-precision binary, i Integer binary
|
-a <increment>
|
Automatically generates X axis values in the given increment |
-B
|
Toggles the line mode |
-m <mode>
|
Defines the line mode; 1 Solid, 2 Dotted, 3 Dot-dashed, 4 Short-dashed, 5 Long-dashed
|
-S <number>
|
Draws a marker symbol at each data point; 1 Dot, 2 Plus sign, 3 Asterisk, 4 Degree, 5 Cross, 0 No symbol
|
-W <thickness>
|
Defines the relative line thickness |
-q <intensity>
|
Defines the shading intensity (fill fraction) of the polygon formed from the line segments |
-C
|
Toggles between color and monochrome dataset rendering |
In creating the command lines, pay particular attention to the order of options and arguments. The options control the presentation of the data values that follow. Apart from the command-line options, Graph provides a set of environmental variables, which the plotutils programs use as options. However, the command-line options override the environmental variables.
Graph reads the input data from a file or standard in. The program expects the data as simple space-separated X/Y value pairs, one for each measurement. Listing 1 shows an example. Note the use of options as comments at the beginning, which can also occur in the data and determine how the data that follows is represented.
Listing 1
Sample Input
#m=5,S=10 1422968301 140736 [...] 11422973527 137436
Alternatively the input can occur in one piece instead of line by line, such as when reading from standard in, as in the following example:
$ echo 1 1 2 2 3 1 1 1| graph -TX
Graph reads in the values as coordinate pairs. You can also use the -a option to specify reading in the values as Y axis values only – more on this later.
Another interpretation results when you use the -I e option to include error bars in output, which are common in normal measurements. In this case, Graph expects data triplets (x y <error> ), unless you enable -a again.
You should first test the data output in an X window (Figure 1). In this case, you can specify X as the output device (graph -T X <data> ) and the program will take care of things automatically. Pressing Q or clicking the window closes it again.
If empty lines appear in the input, Graph interprets them as the start of a new plot and displays the plot correspondingly (Figure 2). The program behaves similarly when data is derived from multiple sources (as shown in the first line of the following example). To distinguish the two plots, give the second one a different color using the -C option as shown in the second line below:
$ graph -TX /tmp/tst /tmp/tst1 $ graph -TX -C /tmp/tst2 /tmp/tst3
See Figures 3 and 4 for examples.
The -a option automatically labels the X axis and can process data with just Y-axis values. This helps when there are constant values or only one coordinate indicated. An example is described in the "Problems with Date Representation" box. The -a option increments the X value for each given Y value. You can add arguments or (if values are missing) let Graph calculate it for you. The two arguments you can set for -a are the first one for the X-axis increment and the second for its lower limit value, as in the example … -a 100 10 … .
Problems with Date Representation
If you're working with time-dependent data, you have two possibilities. The first is to use absolute time with the format string +"%s" , which indicates the number of seconds since the beginning of the Unix epoch, January 1, 1970. The downside is that these values have become quite high over the years. The current date is thereby measured in terms of about 1.4 million seconds when using +"%s" , thus needing to be converted. In scripts, you would use Bc or Dc, in case the shell syntax isn't enough.
The second option is to use other date-formatting strings, such as the %d day number or the %I hour. For this approach, you need to ensure that these values aren't repeated (i.e., you need to delete the input files for Graph often). Alternatively, you can go back to using the -a option in Graph that creates the automatic X-axis values, as long as the distance between values remain the same.
Also interesting is the -q option, whereby Graph fills the polygon formed by the plot line segments with varying shading – with 0.5 halfway between solid and white, as in the following command that generates the plot shown in Figure 5.
$ echo 10 5 5 15 15 15 10 5 | graph -TX -q 0.5
To create a closed surface, the starting and ending plot values have to be identical. The -q argument sets the density of the fill shading, with higher values creating darker shading. Using the -C adds color to the shading. A bit more complicated is filling open curves using -q . In this case, Graph adds a virtual line from the starting and ending points of the curve as the base for the filled surface (Figure 6).
The axes and their labels are important plotting elements, and several options control them. The -E option can make Graph draw the axes on the right or top instead of the usual left and bottom. Using -N <axis> , you can deactivate the usual ticks and their labels for the X or Y axis. In this case, or later, you can relabel each axis with the -X <label> and -Y <label> options (Figure 7).
By default, Graph adds ticks to each axis relative to its origin. Using the -x or -y option, you can be more specific about tick spacing along the axis with the arguments lower_limit , upper_limit , and spacing . If a value is missing or you enter a minus sign, Graph automatically calculates the values from the data. The additional -R <axis> argument tells Graph to round the values to the nearest integer.
Among Graph's special features is its ability to scale an axis logarithmically. You do this with the -l <axis> option, and you can also add a background grid to help orientation (Figure 8).
If the display size isn't big enough, you can use the --bitmap-size option to set a new size. The option takes the <width>x<height> pixels syntax. You can also set the fonts with the -F option and their size with -f . Graph expects a relative value for the size, such as -f .01 , based on the minimum dimension of the plotting box.
Apart from the Graph program, plotutils provides other useful programs. Among the options are Plot , a universal converter for Graph output; Pic2plot , which translates Pic drawings, Plotfont for displaying character maps of fonts; and Spline for "smoothing" data. Additional exotic programs can be used for more specialized tasks.
Spline reads data either from standard input or from a file, processes it, and then writes it out as a data stream in the standard output channel. The data can then be fished out for Graph processing:
$ spline input_data | graph -TX ...
In Figure 9, you can see the example of a curve that was "smoothed" by Spline and output to Graph.
Viewing numerical data as graphs using plotutils is a pleasure – provided the data is in a reasonable format and can be quickly converted. Because of X mode, you don't need to generate an output file, and automatically closing the output window with a mouse click proves ideal for this purpose.
If you want cleanly created graphs for presentations or other purposes, you should definitely consider generating SVG output and then editing it with Inkscape. The original documentation [2] for plotutils reveals many more details.
Infos