Gnuplot

gnuplot
Fig. 1: Gnuplot called from Fortran

Gnuplot is a free plotting tool that can be run from the command-line or invoked by third-party applications. The plotting commands and the input data are read from files. Plots can either be displayed inside a window (fig. 1) or exported in various formats. Gnuplot is under active development since 1986.

The example below writes the computed data to a text file data.txt and then uses the intrinsic Fortran routine system() to call Gnuplot (see also). Additional Fortran modules are not required.

! gnuplot.f90
program main
    implicit none
    character(len=*), parameter :: file_name = 'data.txt'
    integer,          parameter :: fh        = 10
    integer,          parameter :: n         = 10

    real, dimension(10) :: x, y
    integer             :: i

    x(1) = 0.0
    y(1) = 0.0

    do i = 2, n
        x(i) = 0.1 * i
        y(i) = x(i) * x(i)
    enddo

    open(unit=fh, file=file_name)

    do i = 1, n
        write (fh, *) x(i), y(i)
    enddo

    close(fh)

    call system('gnuplot -p data_plot.plt')
end program main

The Gnuplot commands to display the plot are stored in file data_plot.plt:

# data_plot.plt
set xlabel "x"
set ylabel "y"
m="data.txt"
set terminal x11 0
set nokey
set grid
set title 'Fortran Example'
plot m using 1:2 with linespoints

This file will be used by Gnuplot to do the actual plotting.

Fortran Libraries

Several implementations of Gnuplot libraries are available: