Next: , Previous: graph, Up: graph

2.1 Simple examples using graph

By default, graph reads ASCII data from the files specified on the command line, or from standard input if no files are specified. The data are pairs of numbers, interpreted as the x and y coordinates of data points. An example would be:

     0.0  0.0
     1.0  0.2
     2.0  0.0
     3.0  0.4
     4.0  0.2
     5.0  0.6

Data points do not need to be on different lines, nor do the x and y coordinates of a data point need to be on the same line. However, there should be no blank lines in the input if it is to be viewed as forming a single dataset.

To plot such a dataset with graph, you could do

     graph -T ps datafile >

or equivalently

     graph -T ps < datafile >

Either of these would produce an encapsulated Postscript file, which could be sent to a printer, displayed on a screen by the Postscript viewer gv, or edited with the free drawing editor idraw. The ‘--page-size’ option, or equivalently the PAGESIZE environment variable, specifies the size of the page on which the plot will be positioned. The default is "letter", i.e., 8.5in by 11in, but "a4" or other ISO or ANSI page sizes could equally well be specified. See Page and Viewport Sizes.

Similarly, you would do

     graph -T svg < datafile > plot.svg
     graph -T cgm < datafile > plot.cgm

to produce SVG and WebCGM files that could be displayed in a Web browser with SVG and WebCGM support, or

     graph -T fig < datafile > plot.fig

to produce a file plot.fig in Fig format that could be edited with the free xfig drawing editor, or

     graph -T ai < datafile >

to produce a file that could be edited with Adobe Illustrator. If you do

     graph -T hpgl < datafile > plot.plt

you will produce a file plot.plt in the Hewlett–Packard Graphics Language (HP-GL/2) that may be sent to a Hewlett–Packard plotter. Similarly, you would use graph -T pcl to produce a file in PCL 5 format that may be printed on a LaserJet or other laser printer.

You would use graph -T X to pop up a window on an X Window System display, and display the plot in it. For that, you would do

     graph -T X < datafile

If you use graph -T X, no output file will be produced: only a window. The window will vanish if you type ‘q or click your mouse in it.

You may also use graph -T png to produce a PNG file, graph -T pnm to produce a PNM file (a “portable anymap”), and graph -T gif to produce a pseudo-GIF file. If the free image display application display is available on your system, you could use any of the three commands

     graph -T png < datafile | display
     graph -T pnm < datafile | display
     graph -T gif < datafile | display

to view the output file.

Another thing you can do is use graph -T tek to display a plot on a device that can emulate a Tektronix 4014 graphics terminal. xterm, the X Window System terminal emulator, can do this. Within an xterm window, you would type

     graph -T tek < datafile

xterm normally emulates a VT100 terminal, but when this command is issued from within it, it will pop up a second window (a `Tektronix window') and draw the plot in it. The Japanese terminal emulator kterm should be able to do the same, provided that it is correctly installed. Another piece of software that can emulate a Tektronix 4014 terminal is the MS-DOS version of kermit.

In the same way, you would use graph -T regis to display a plot on any graphics terminal or emulator that supports ReGIS graphics. dxterm, the DECwindows terminal emulator, can do this. Several DEC terminals (in particular the VT340, VT330, VT241, and VT240 terminals) also support ReGIS graphics.

graph may behave differently depending on the environment in which it is invoked. We have already mentioned the PAGESIZE environment variable, which affects the operation of graph -T svg, graph -T ai, graph -T ps, graph -T cgm, graph -T fig, graph -T pcl, and graph -T hpgl. Similarly, the BITMAPSIZE environment variable affects the operation of graph -T X, graph -T png, graph -T pnm, and graph -T gif. The DISPLAY environment variable affects the operation of graph -T X, and the TERM environment variable affects the operation of graph -T tek. There are also several environment variables that affect the operation of graph -T pcl and graph -T hpgl. For a complete discussion of the effects of the environment on graph, see graph Environment. The following remarks apply irrespective of which output format is specified.

By default, successive points in the dataset are joined by solid line segments, which form a polygonal line or polyline that we call simply a `line'. You may choose the style of line (the `linemode') with the ‘-m’ option:

     graph -T ps -m 2 < datafile >

Here ‘-m 2’ indicates that linemode #2 should be used. If the dataset is rendered in monochrome, which is the default, the line can be drawn in one of five distinct styles. Linemodes #1 through #5 signify solid, dotted, dotdashed, shortdashed, and longdashed; thereafter the sequence repeats. If the-C’ option is used, the dataset will be rendered in color. For colored datasets, the line can be drawn in one of 25 distinct styles. Linemodes #1 through #5 signify red, green, blue, magenta, and cyan; all are solid. Linemodes #6 through #10 signify the same five colors, but dotted rather than solid. Linemodes #11 through #16 signify the same five colors, but dotdashed, and so forth. After linemode #25, the sequence repeats. Linemode #0, irrespective of whether the rendering is in monochrome or color, means that the line is not drawn.

You may wish to fill the polygon bounded by the line (i.e., shade it, or fill it with a solid color). For this, you would use the ‘-q’ option. For example,

     echo .1 .1 .1 .9 .9 .9 .9 .1 .1 .1 |
         graph -T ps -C -m 1 -q 0.3 >

will plot a square region with vertices (0.1,0.1), (0.1,0.9), (0.9,0.9), and (0.9,0.1). The repetition of the first vertex (0.1,0.1) at the end of the sequence of vertices ensures that the square will be closed: all four segments of its boundary will be drawn. The square will be drawn in red, since the colored version of linemode #1 is requested. The interior of the square will be filled with red to an intensity of 30%, as the ‘-q 0.3’ option specifies. If the intensity were 1.0, the region would be filled with solid color, and if it were 0.0, the region would be filled with white. If the intensity were negative, the region would be unfilled, or transparent (the default).

You may specify the thickness (`width') of the line, whether it is filled or not, by using the ‘-W’ option. For example, ‘-W 0.01’ specifies that the line should have a thickness equal to 0.01 times the size of the graphics display. Also, you may put symbols at each data point along the line by doing, for example,

     graph -T ps -S 3 0.1 < datafile >

where the first argument 3 indicates which symbol to plot. The optional second argument 0.1 specifies the symbol size as a fraction of the size of the `plotting box': the square within which the plot is drawn. Symbol #1 is a dot, symbol #2 is a plus sign, symbol #3 is an asterisk, symbol #4 is a circle, symbol #5 is a cross, and so forth. (See Marker Symbols.) Symbols 1 through 31 are the same for all display types, and the color of a symbol will be the same as the color of the line it is plotted along.

Actually, you would probably not want to plot symbols at each point in the dataset unless you turn off the line joining the points. For this purpose, the `negative linemode' concept is useful. A line whose linemode is negative is not visible; however, any symbols plotted along it will have the color associated with the corresponding positive linemode. So, for example,

     graph -T ps -C -m -3 -S 4 < datafile >

will plot a blue circle at each data point. The circles will not be joined by line segments. By adding the optional second argument to the ‘-S’ option, you may adjust the size of the circles.

graph will automatically generate abscissa (i.e., x) values for you if you use the ‘-a’ option. If this option is used, no abscissa values should be given in the data file. The data points will be taken to be regularly spaced along the abscissa. The two arguments following ‘-a’ on the command line will be taken as the sampling interval and the abscissa value of the first data point. If they are absent, they default to 1.0 and 0.0 respectively. For example, the command

     echo 0 1 0 | graph -T ps -a >

produces exactly the same plot as

     echo 0 0 1 1 2 0 | graph -T ps >

If the ‘-I e’ option is specified, graph will plot data with error bars. In this case the dataset should consist of triples (x,y,error), rather than pairs (x,y). A vertical error bar of the appropriate length will be plotted at each data point. You would plot a symbol at each data point, along with the error bar, by using the ‘-S’ option in the usual way. The symbol will be the same for each point in the dataset. You may use the ‘-a’ option in conjunction with ‘-I e’, if you wish. If you do, the dataset should contain no abscissa (i.e., x) values.

By default, the limits on the x and y axes, and the spacing between the labeled ticks on each axis, are computed automatically. You may wish to set them manually. You would accomplish this with the ‘-x’ and ‘-y’ options.

     echo 0 0 1 1 2 0 | graph -T ps -x -1 3 -y -1 2 >

will produce a plot in which the x axis extends from −1 to 3, and the y axis from −1 to 2. By default, graph tries to place about six numbered ticks on each axis. By including an optional third argument to ‘-x’ or ‘-y’, you may manually set the spacing of the labeled ticks. For example, using ‘-y -1 2 1’ rather than ‘-y -1 2’ will produce a y axis with labeled ticks at −1, 0, 1, and 2, rather than at the locations that graph would choose by default, which would be −1, −0.5, 0, 0.5, 1, 1.5, and 2. In general, if a third argument is present then labeled ticks will be placed at each of its integer multiples.

To make an axis logarithmic, you would use the ‘-l’ option. For example,

     echo 1 1 2 3 3 1 | graph -T ps -l x >

will produce a plot in which the x axis is logarithmic, but the y axis is linear. To make both axes logarithmic, you would use ‘-l x -l y’. By default, the upper and lower limits on a logarithmic axis are powers of ten, and there are tick marks at each power of ten and at its integer multiples. The tick marks at the powers of ten are labeled. If the axis spans more than five orders of magnitude, the tick marks at the integer multiples are omitted.

If you have an unusually short logarithmic axis, you may need to increase the number of labeled ticks. To do this, you should specify a tick spacing manually. For example, ‘-l x -x 1 9 2’ would produce a plot in which the x axis is logarithmic and extends from 1 to 9. Labeled ticks would be located at each integer multiple of 2, i.e., at 2, 4, 6, and 8.

You would label the x and y axes with the ‘-X’ and ‘-Y’ options, respectively. For example,

     echo 1 1 2 3 3 1 | graph -T ps -l x -X "A Logarithmic Axis" >

will label the log axis in the preceding example. By default, the label for the y axis (if any) will be rotated 90 degrees, unless you use the ‘-Q’ option. (Some X Window System displays, both old and new, do not properly support rotated labels, and require the ‘-Q’ option.) You may specify a `top label', or title for the plot, by using the ‘-L’ option. Doing, for example,

     echo 1 1 2 3 3 1 | graph -T ps -l x -L "A Simple Example" >

will produce a plot with a title on top.

The font size of the x axis and y axis labels may be specified with the ‘-f’ option, and the font size of the title with the ‘--title-font-size’ option. For example,

     echo 1 1 2 3 3 1 | graph -T ps -X "Abscissa" -f 0.1 >

will produce a plot in which the font size of the x axis label, and each of the numerical tick labels, is very large (0.1 times the size of the plotting box, i.e., the square within which the plot is drawn).

The font in which the labels specified with the ‘-X’, ‘-Y’, and ‘-L’ options are drawn can be specified with the ‘-F’ option. For example, ‘-F Times-Roman’ will make the labels appear in Times-Roman instead of the default font (which is Helvetica, unless ‘-T png’, ‘-T pnm’, ‘-T gif’, ‘-T pcl’, ‘-T hpgl’, ‘-T regis’, or ‘-T tek’ is specified). Font names are case-insensitive, so ‘-F times-roman’ will work equally well. The available fonts include 35 Postscript fonts (for all variants of graph other than graph -T png, graph -T pnm, graph -T gif, graph -T pcl, graph -T hpgl, graph -T regis, and graph -T tek), 45 PCL 5 fonts (for graph -T svg, graph -T ai, graph -T pcl and graph -T hpgl), a number of Hewlett–Packard vector fonts (for graph -T pcl and graph -T hpgl), and 22 Hershey vector fonts. The Hershey fonts include HersheyCyrillic, for Russian, and HersheyEUC, for Japanese. For a discussion of the available fonts, see Text Fonts. The plotfont utility will produce a character map of any available font. See plotfont.

The format of the labels drawn with the ‘-X’, ‘-Y’, and ‘-L’ options may be quite intricate. Subscripts, superscripts, square roots, and switching fonts within a typeface are all allowed. The above examples do not illustrate this, but for details, see Text String Format.

Each of the preceding examples produces a plot containing the default sort of grid (a square plotting box, with ticks and labels drawn along its lower edge and its left edge). There are actually several sorts of grid you may request. The ‘-g 0’, ‘-g 1’, ‘-g 2’, and ‘-g 3’ options yield successively fancier grids. What they yield, respectively, is no grid at all, a pair of axes with ticks and labels, a square plotting box with ticks and labels, and a square plotting box with ticks, labels, and grid lines. As you can check, ‘-g 2’ is the default. There is also a ‘-g 4’ option, which yields a slightly different sort of grid: a pair of axes that cross at the origin. This last sort of grid is useful when the x or y coordinates of the data points you are plotting are both positive and negative.