PLplot

PLplot is a multi-platform scientific plotting library written in C, with bindings available for a broad range of programming languages, such as C++, Java, Lua, Octave, Python, but Fortran as well. PLplot features non-interactive and interactive device drivers, i. e., the output is either written to file in PNG, SVG, PDF, and other formats, or shown in a window (X11, Qt, GTK). The library is licenced under LGPL.

The core library provides routines to create XY plots, semi-log plots, log-log plots, contour plots, 3D surface plots, mesh plots, bar charts, and pie charts. It is possible to place multiple graphs on a single page, and multiple pages are allowed for those devices that support them.

PLplot example
Fig. 1: Example output of PLplot

Installation

PLplot is available as a package on most Unix-like operating systems. Just install package math/plplot on FreeBSD:

# pkg install math/plplot

Make sure that the package has to be built with Fortran support. Any Fortran application has to be linked either against -lpgplot (shared library) or libpgplot.a (static library) to access the PLplot procedures, additionally to -lX11. The PLplot package includes a selection of example programs in directory /usr/local/share/plplot5.15.0/examples/fortran/ (FreeBSD).

Example

The following very basic example simply plots a sine wave using PLplot (fig. 1).

! plot.f90
program plot
    use :: plplot
    implicit none
    integer, parameter :: N     = 2000
    real,    parameter :: XSTEP = 0.01

    real    :: x(N), y(N)
    real    :: xmax, xmin, ymax, ymin
    integer :: i, rc

    xmin = 0.0
    xmax = XSTEP * N
    ymin = -10.0
    ymax = 10.0

    ! Prepare data.
    do i = 1, N
        x(i) = XSTEP * i
        y(i) = 7.5 * sin(x(i))
    end do

    ! Parse and process command line arguments.
    rc = plparseopts(PL_PARSE_FULL)
    if (rc /= 0) stop 'Error: Parsing command-line arguments failed'

    call plscolbg(16, 16, 16) ! Set background colour (RGB).
    call plinit()             ! Initialise PLplot.
    call plcol0(15)           ! Set foreground colour.

    ! Create a labeled box that contains the plot.
    call plenv(xmin, xmax, ymin, ymax, 0, 0)
    call pllab('x', 'y = 7.5 sin(x)', '2D line plot using PLplot' )

    ! Plot the data as line diagram.
    call plline(x, y)

    ! Close PLplot library
    call plend()
end program plot

Compile and link the example against -lplplotfortran:

$ gfortran10 -I/usr/local/lib/fortran/modules/plplot/ -o plot plot.f90 -lplplotfortran

Execute the binary with desired output device and plot size:

$ ./plot -dev xwin -geometry 640x400

Passing the command-line argument -h prints all available options to stdout:

$ ./plot -h

Usage:
        ./plot [options]

PLplot options:
    -h                   Print out this message
    -v                   Print out the PLplot library version number
    -verbose             Be more verbose than usual
    -debug               Print debugging info (implies -verbose)
    -dev name            Output device name
    -o name              Output filename
    -display name        X server to contact
    -px number           Plots per page in x
    -py number           Plots per page in y
    -geometry geom       Window size/position specified as in X, e.g., 400x300, 400x300-100+200, +100-200, etc.
    -wplt xl,yl,xr,yr    Relative coordinates [0-1] of window into plot
    -mar margin          Margin space in relative coordinates (0 to 0.5, def 0)
    -a aspect            Page aspect ratio (def: same as output device)
    -jx justx            Page justification in x (-0.5 to 0.5, def 0)
    -jy justy            Page justification in y (-0.5 to 0.5, def 0)
    -ori orient          Plot orientation (0,1,2,3=landscape,portrait,seascape,upside-down)
[...]

Output Devices

The supported output device drivers are selected through command-line argument -dev (tab. 1).

ParameterDescription
xwinX window (Xlib)
psPostScript file (monochrome)
pscPostScript file (color)
xfigFig file
nullNull device
memUser-supplied memory device
psttfPostScript file (monochrome)
psttfcPostScript file (color)
svgScalable Vector Graphics (SVG 1.1)
xcairoCairo X window driver
pdfcairoCairo PDF driver
pscairoCairo PostScript driver
epscairoCairo EPS driver
svgcairoCairo SVG driver
pngcairoCairo PNG driver
memcairoCairo memory driver
extcairoCairo external Context driver
Tab. 1: Selection of output devices supported by PLplot

References