There are other modules and libraries available for calling gnuplot(1) from Fortran, but I prefer to use the M_process(3f) module from the GPF (General Purpose Fortran) collection, as illustrated below.
I find that when calling a line-mode program from another program the most natural way is to open a process and write to it.
Following is an example program that calls the M_process module to start a plotting program called gnuplot(1) and give it enough commands to generate a plot. It then lets you interactively interact with the gnuplot(1) program or continue on in the program.
program gnuplotExample
! @(#) Example of Fortran writing GNUPLOT command and data file.
use M_process ,only: process_open_write, process_writeline
use M_process ,only: streampointer, process_close
implicit none
character(len=4096) :: line !*! line of data to write (assumed long enough to hold any command line)
type(streampointer) :: fp !*! C file pointer returned by process_open()
integer :: ierr !*! check status of calls to process module routines
integer :: i !*! DO loop counter
integer,parameter :: n=50 !*! number of points to put into curve to be plotted
real :: x(n),y(n) !*! arrays to fill with curve data to be plotted
integer :: ios
!*! Define sample X,Y array.
do i=1,n !*! set X() values as whole numbers 1 to N
x(i)=i
!*!
y(i)=(x(i)+0.5)**2
enddo
call plotit()
write(*,'(a)')'RETURNED TO THE MAIN PROGRAM'
!*! Write the GnuPlot commands
contains
subroutine plotit()
!-----------------------------------------------------------------------------------------------------------------------------------
call process_open_write('gnuplot',fp,ierr) !*! open process to write to (ie. start gnuplot(1) program)
!-----------------------------------------------------------------------------------------------------------------------------------
call process_writeline('$SET1 <<EOD',fp,ierr) !*! create in-line dataset $SET1
do i=1,n
write(line,'(2(f10.3,1x))')x(i),y(i) !*! Write the X,Y array as coordinates to be plotted.
call process_writeline(line,fp,ierr)
enddo
call process_writeline([character(len=128) :: &
&'EOD ', &
&'set title " Example of GNUPlot data and command file generation"', &
&'set nokey' , &
&'plot $SET1 with lines' , &
&''],fp,ierr)
!-----------------------------------------------------------------------------------------------------------------------------------
write(*,'(a)')'enter gnuplot commands or "." to exit' !*! Additional gnuplot commands; in this case interactively entered
do
write(*,'(a)',advance='no')'gnu>>'
read(*,'(a)',iostat=ios)line
if(line.eq.'.')exit
call process_writeline(trim(line),fp,ierr)
enddo
!-----------------------------------------------------------------------------------------------------------------------------------
call process_close(fp,ierr) !*! Wrap up
write(*,*)'CLOSED THE PROCESS. RETURNING TO PROGRAM'
!-----------------------------------------------------------------------------------------------------------------------------------
end subroutine plotit
end program gnuplotExample