next up previous
Next: X-Y Plots in C Up: A Mug's guide to Previous: A Mug's guide to

X-Y Plots in FORTRAN

The following FORTRAN program is an example in which we plot sin(0.5nx)/n vs x for x=0 to 5 and n=1,2,3,4,5. We will do this by defining arrays x() and y() and calculating the value of y at each incremental step of 0.1 in x. These arrays are then passed on to the PGPLOT subroutine pgline to plot the curves.

      program example1
c
c The number of curves to plot is ncurves, each consisting of npts points.
c
      integer ncurves,npts
      parameter(ncurves=5)
      parameter(npts=51)
c
c The array x() holds the values of x, while y() holds the various values
c   of y that correspond to each value of x.
c   The integers i and j are dummy indices to keep track of where we are.
c
      real x(npts),y(npts)
      integer i,j
c
c Initialize the PGPLOT package with a call to PGBEGIN. This must be done
c   before any other calls to PGPLOT subroutines. The syntax is:
c
c        call pgbegin(irr,device,nxsub,nysub)
c
c   irr    - an integer which is irrelevent and is ignored by PGPLOT.
c              It is always set to zero.
c   device - a string which specifies the output graphics device.
c              Examples of valid devices are:
c                '/xwindow' - for your X-terminal screen.
c                '/tek'     - for your tektronix screen.
c                '/ps'      - for a landscape-oriented postscript file.
c                '/vps'     - for a portrait-oriented postscript file.
c                '?'        - PGPLOT will prompt you to input a device
c                               at runtime.
c   nxsub  - an integer for the number of horizontal subdivisions of the
c              plotting screen.
c   nysub  - an integer for the number of vertical subdivisions of the
c              plotting screen.
c
      call pgbegin(0,'?',1,1)
c
c Draw axes on the plotting screen with a call to PGENV. The syntax is:
c
c       call pgenv(xmin,xmax,ymin,ymax,just,axis)
c
c   xmin,xmax,ymin,ymax
c          - reals which define the boundaries of the plotting axes.
c   just   - an integer describing the justification of the axes:
c              just=1 will cause the x- and y-axes to be scaled equally;
c              just=0 will cause the axes to be scaled independently so
c                       that the graph will fill the entire screen.
c   axis   - an integer describing the type of axes desired for the graph:
c              axis=-2 draws no box, axes or labels;
c              axis=-1 draws a box only;
c              axis= 0 draws a box and labels it with coordinates;
c              axis= 1 draws a box with coordinate labels as well as
c                        the (X=0,Y=0) coordinate axes;
c              axis= 2 draws a box with coordinate labels, the coordinate
c                        axes and grid lines at major increments of the
c                        coordinates;
c              axis=10 draws a box with the x-axis labelled logarithmically
c              axis=20 draws a box with the y-axis labelled logarithmically
c              axis=30 draws a box with both axes labelled logarithmically
c
      call pgenv(0.0,5.0,-1.0,1.0,0,1)
c
c Put labels on the axis and at the top of the graph with a call to
c   PGLABEL. The syntax is:
c
c       call pglabel(xlabel,ylabel,toplabel)
c
c   xlabel,ylabel,toplabel
c         - strings for the labels for respectively, the x-axis, the y-axis
c             and at the top of the graph. A blank string '' denotes the
c             abscence of a label.
c
      call pglabel('x-axis','y-axis','My first graph')
c
c Do calculations and plot each curve with a call to PGLINE. We will need
c   arrays of points defining the curve. PGLINE joins the dots on the curve
c   with straight line segments. The syntax is:
c
c       call pgline(npts,xpts,ypts)
c
c   npts  - an integer for the number of points which define the curve.
c   xpts,ypts
c         - real arrays with the x- and y-coordinates of the points which define
c             the curve.
c
      do 20 j=1,ncurves
        do 10 i=1,npts
          x(i)=0.1*(i-1)
          y(i)=sin(0.5*j*x(i))*(1.0/j)
10      continue
        call pgline(npts,x,y)
20    continue
c
c Terminate PGPLOT with a call to PGEND. This is crucial, especially with
c   postscript output files, to ensure that the output buffer is flushed.
c
      call pgend
c
      stop
      end



Daniel Mitchell
1999-03-01