Graphing with Gnuplot and Xmgr
Graphing data is one of the oldest uses for a computer, dating back to FORTRAN programs producing character graphics on line-printers. Fortunately, things have advanced somewhat, and modern computers are capable of producing much nicer graphs. Several graphing packages are available for Linux under X and SVGALIB. Two of the most prominent packages are gnuplot and xmgr (a.k.a. ACE/gr). Xmgr is oriented towards graphing and manipulation of externally produced data sets, while gnuplot is used more for plotting data and mathematical functions.
Gnuplot's primary authors are Thomas Williams and Colin Kelley, with many others contributing. Although gnuplot was written independently of the Free Software Foundation, the FSF does distribute it. Gnuplot was written with portability in mind, supporting about four dozen output devices and formats under a dozen operating systems. Under Linux, it will run under both X and SVGALIB. Modifying gnuplot to support a new device involves writing a few device-dependent subroutines that are linked in with the main program.
Xmgr, on the other hand, is tied to X. Developed by Paul Turner, it also runs on many platforms besides Linux, but it outputs only PostScript. In the latter stages of development, Linux was the primary development platform. Development has recently been spread around to a loose organization of interested people.
Gnuplot has a command-line interface with a mixture of emacs and Unix command line editing similar to the bash shell. Gnuplot may be run in batch mode, where the commands are taken from a file. The plot command causes a plot to be sent to the currently selected device. In the case of the Linux svgalib driver, a graphics mode is selected and a graph is drawn in the current virtual console. When a key is hit, the display changes back to text mode for an additional command. Under X, a new window is created for the graph, while commands are entered in the original shell window.
Gnuplot has a comprehensive on-line help facility that can be accessed by typing help. The basic help command lists arguments of the help command by topic. Some subjects, like the set command, have many sub-topics. The documentation itself is well written and has many valuable examples of working commands.
A datafile containing points to plot is identified by the file name in single or double quotes. Each line has a two or more space-separated numbers that correspond to a point that is to be plotted. For example, suppose we had a file named “hits”:
# Monthly hits on our web site 1 13 2 23 3 66 4 75 5 74 6 82 7 377 8 442 9 512 10 756 11 874 12 946
The command plot "hits" would plot a graph of the data in the file named hits. Lines in a data file beginning with a # character are treated as comment lines. Blank lines are not treated as comments. Instead, they indicate where a line should not be drawn between a pair of points.
Although our example has the x data listed in the first column and the y in the second, gnuplot can handle cases where this is not so. The command:
plot "hits" using 2:1
would cause the x data to be read from the second column and the y data from the first column.
Plots can be embellished in many ways. Each comma-separated file or mathematical expression (shown later) to plot has two attributes that can be specified by the user: a title and a style. A gnuplot “title” is a string that is displayed with an example of the plot style that labels that data; this is usually called a “legend” by other programs. The style of the plot is selected from several possible ones, including “points”, which displays a symbol at each data point, “lines”, which draws lines between the points, and “linespoints”, which draws both the lines and the symbols. The color of the line and symbol as well as the type of symbol (plus sign, cross, box) are normally assigned in series by gnuplot to make each distinct, but these can be overridden by the user.
For example, the command plot "hits" title 'Hits on Website' with linespoints 3 4 plots our data file using lines of type 3 and points of type 4. At the top right will be the string Hits on Website next to a short example of type 3 lines and type 4 points. What you actually see depends on the output device being used—lines that are colored on a color display can come out dashed and dotted on monochrome devices (like most PostScript printers).
Our plot is looking better, but it is still not perfect. We want to put labels on the x and y axes to further clue the reader in on what we are looking at. Axis labels are setable parameters, as is the graph title:
set xlabel "Month" set ylabel "Hits" set title "Hits on the Website" replot
Experimentation is easy to do in gnuplot by using the replot command, which repeats the previous plot command. Not only does this save keystrokes, but the author has a friend who likes to type replot repeatedly to display a file being appended to by another job he is running, which gives a running display of results as they are calculated.
Our graph is almost finished. Gnuplot's default algorithm for deciding where the x tick marks appear is showing only every other x point. We can make it show them all by:
set xtics 1, 1 replot
The first number causes the tick marks to start at x=1, and the second causes them to be spaced one unit apart. We could have included a third comma-separated parameter to indicate where the last tick mark should be plotted, but it is unnecessary in our example.
We can do better than month numbers:
set xtics ("Jan" 1, "Feb" 2, "Mar" 3, "Apr" 4, "May" 5, "Jun" 6, "Jul" 7, "Aug" 8, "Sep" 9, "Oct" 10, "Nov" 11, "Dec" 12) replot
We have arrived at a graph worthy of being shown to the boss. The result is shown in Figure 1.
All that remains is to print it out. Gnuplot treats printers and plotters as just another output device. Executing the command:
set terminal PostScript
tells gnuplot to generate PostScript of the graph instead of console graphics. It is not enough to set the type of terminal. Typing replot now will cause gnuplot to spew PostScript to the user terminal. The command:
set output "graph.ps" replot
will cause PostScript to be sent to the file graph.ps. If the first character of the filename is a vertical bar, gnuplot interprets the rest of the string as a program that will accept gnuplot's output as its standard input. So a command like:
set output "|lp" replot
sends the output to the system's default printer.
Plots of mathematical functions are easy to produce: plot 2*x will produce a plot of a line with a slope of two on the default range of -10.0 to +10.0. The y-axis is automatically scaled by default so that all points are visible. For a mathematical function, the x range is taken from a default range. Multiple plots can be overlaid, with separate expressions separated by a comma.
A wide variety of common mathematical functions can be used in expressions—trigonometric, exponential and logarithmic as well as less common functions such as bessel functions and error functions. Expressions are based mostly on C-style expressions including the logical AND (&&) and OR (||) operators with the notable addition of the FORTRAN power operator (**).
Ranges are specified by a pair of numbers separated by a colon enclosed in square braces. Either or both numbers may be omitted to avoid affecting the current default. The first number specifies the range to begin and the second specifies the end. If we wanted to look at several graphs with the same range, the default range can be changed with the command set xrange [1:2]. If we wanted to change the range in only one plot, a range can be specified before the first function being plotted.
Fast/Flexible Linux OS Recovery
On Demand Now
In this live one-hour webinar, learn how to enhance your existing backup strategies for complete disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible full-system recovery solution for UNIX and Linux systems.
Join Linux Journal's Shawn Powers and David Huffman, President/CEO, Storix, Inc.
Free to Linux Journal readers.Register Now!
- Download "Linux Management with Red Hat Satellite: Measuring Business Impact and ROI"
- Petros Koutoupis' RapidDisk
- ServersCheck's Thermal Imaging Camera Sensor
- The Italian Army Switches to LibreOffice
- Linux Mint 18
- Oracle vs. Google: Round 2
- The FBI and the Mozilla Foundation Lock Horns over Known Security Hole
- Firefox 46.0 Released
- Varnish Software's Varnish Massive Storage Engine
Until recently, IBM’s Power Platform was looked upon as being the system that hosted IBM’s flavor of UNIX and proprietary operating system called IBM i. These servers often are found in medium-size businesses running ERP, CRM and financials for on-premise customers. By enabling the Power platform to run the Linux OS, IBM now has positioned Power to be the platform of choice for those already running Linux that are facing scalability issues, especially customers looking at analytics, big data or cloud computing.
￼Running Linux on IBM’s Power hardware offers some obvious benefits, including improved processing speed and memory bandwidth, inherent security, and simpler deployment and management. But if you look beyond the impressive architecture, you’ll also find an open ecosystem that has given rise to a strong, innovative community, as well as an inventory of system and network management applications that really help leverage the benefits offered by running Linux on Power.Get the Guide