A graph is composed of several elements: a box or axes delineating the graph and indicating the scale, labels if required, and one or more points or lines. To draw a graph you need to call at least four of the PGPLOT functions and subroutines:
PGBEG, to start up PGPLOT and specify the device you want to plot on;
PGENV, to define the range and scale of the graph, and draw labels, axes etc;
PGLINEor both, or other drawing routines, to draw points or lines.
PGENDto close the plot.
PGENDonce each, unless you want to plot on more than one device.
This chapter presents a very simple example program to demonstrate the above four steps.
PROGRAM SIMPLE INTEGER I, IER, PGBEG REAL XR(100), YR(100) REAL XS(5), YS(5) DATA XS/1.,2.,3.,4.,5./ DATA YS/1.,4.,9.,16.,25./ IER = PGBEG(0,'?',1,1) IF (IER.NE.1) STOP CALL PGENV(0.,10.,0.,20.,0,1) CALL PGLAB('(x)', '(y)', 'A Simple Graph') CALL PGPT(5,XS,YS,9) DO 10 I=1,60 XR(I) = 0.1*I YR(I) = XR(I)**2 10 CONTINUE CALL PGLINE(60,XR,YR) CALL PGEND ENDThe following sections of this chapter describe how the program works, and the resulting plot is shown in Figure 2.1.
YS. For convenience, this program defines the values in
DATAstatements, but a more realistic program might read them from a file. Arrays
YRwill be used later in the program for the theoretical curve.
REAL XR(100), YR(100) REAL XS(5), YS(5) DATA XS/1.,2.,3.,4.,5./ DATA YS/1.,4.,9.,16.,25./
INTEGER PGBEG IER = PGBEG(0,'?',1,1) IF (IER.NE.1) STOPNote that
PGBEGis a Fortran function, not a subroutine, and must be declared
INTEGER. It has four arguments, and returns an integer code which will have value 1 if the device was opened successfully.
'?', the program will ask the user to supply the device specification at run-time.
PGENVstarts a new picture and defines the range of variables and the scale of the plot.
PGENValso draws and labels the enclosing box and the axes if requested. In this case, the x-axis of the plot will run from 0.0 to 10.0 and the y-axis will run from 0.0 to 20.0.
PGENVhas six arguments:
PGLABmay (optionally) be called after
PGENVto write identifying labels on the x and y axes, and at the top of the picture:
CALL PGLAB('(x)', '(y)', 'A Simple Graph')All three arguments are character variables or constants; any of them can be blank (
PGPTdraws graph markers at one or more points on the graph. Here we use it to mark the five data points:
CALL PGPT(5,XS,YS,9)If any of the specified points fall outside the window defined in the call to
PGENV, they will not be plotted. The arguments to
DO 10 I=1,60 XR(I) = 0.1*I YR(I) = XR(I)**2 10 CONTINUE CALL PGLINE(60,XR,YR)We compute the x and y coordinates at 60 points on the theoretical curve, and use subroutine
PGLINEto draw a curve through them.
PGLINEjoins up the points with straight-line segments, so it is necessary to compute coordinates at fairly close intervals in order to get a smooth curve. Any lines which cross the boundary of the window defined in
PGENVare ``clipped'' at the boundary, and lines which lie outside the boundary are not drawn. The arguments of
PGLINEare like those of
PGENDmust be called to complete the graph properly, otherwise some pending output may not get sent to the device:
emacs simple.f ... f77 -o simple simple.f -lpgplot -lX11Under VMS:
$ EDIT SIMPLE.FOR ... $ FORTRAN SIMPLE $ LINK SIMPLEWhen you run the program, it will ask you to supply the graphics device specification. Type in any allowed device specification, or type a question-mark (
?) to get a list of the available device types. For example, if you are using an X Window display, type
/XWIN: the graph will appear on the terminal screen.
If you want a hard copy, you can run the program again, and specify a
different device type, e.g.,
simple.ps/PS to make a disk
file in PostScript format. To obtain the hard copy, print the file
(but first check with your system manager what the correct print
command is; it is possible to waste a lot of paper by using the wrong
command or sending a file to the wrong sort of printer!).