Ogpf
ogpf is Object based interface to GnuPlot from Fortran 2003, 2008 and later
Install / Use
/learn @kookma/OgpfREADME
ogpf
Object Based Interface to GnuPlot from Fortran (ogpf)
Installation
Prerequisite: gnuplot must be installed on your system.
Zero Installation Library
The ogpf is a zero installation library! Just copy and paste the ogpf.f90 in your project folder or in your library folder and use it! No any further step is required. However if you like to add ogpf to your projects through package managers, builders, installers see below options!
</details> <details> <summary><b>Meson Builder</b></summary>To use ogpf with your fpm project, add the following to your package manifest file (
fpm.toml):[dependencies] ogpf = { git = "https://github.com/kookma/ogpf.git" }You can then
usethe package as normal in your Fortran program withuse ogpf.To run the example program in this package with fpm:
$ git clone https://github.com/kookma/ogpf.git $ cd ogpf $ fpm build $ fpm run --example
</details>The alternative is through the use of the Meson builder, which is multiplatform and multi source language build system. You can just use the ogpf git repo as a subproject in your own meson project. If you're unfamiliar with it, just read this begginer's guide.
After learning the basics, you can create a folder called subprojects on the root of your project and create a file inside with the following content:
[wrap-git] url=https://github.com/kookma/ogpf revision=headThen on the meson.build file you have to import the subproject and grab the library variable. This is a sample meson.build file:
project('research','fortran') ogpf_proj = subproject('ogpf') ogpf_dep = ogpf_proj.get_variable('ogpf_dep') executable('research', dependencies: ogpf_dep)and all the compiler flags will be handled.
Usage
Make sure you have a working version of gnuplot must be installed on your system. Start with provideed examples.
2D Plots
Simple plot | Animation
:-----------------------------:|:------------------------------------:
| 
3D Plots
Surface | Contour
:-----------------------------:|:------------------------------------:
| 
GnuPlot Interface
Purpose: Object Based Interface to GnuPlot from Fortran (ogpf)
Platform: Windows XP/Vista/7/10, Linux
(It should work on other platforms, e.g Mac see the finalize_plot subroutine in ogpf.f90)
Language: Fortran 2003 and 2008
Requires: 1. Fortran 2003 compiler (e.g gfortran 4.7, IVF 12.1, and later ...)
There is only two more features needs Fortran 2008 standard
execute_command_line and passing internal function as argument.
2. gnuplot 5 and higher (other previous version can be used)
Author: Mohammad Rahmani
Chem Eng Dep., Amirkabir Uni. of Tech
Tehran, Ir
url: aut.ac.ir/m.rahmani
github: github.com/kookma
email: m[dot]rahmani[at]aut[dot]ac[dot]ir
License: MIT. Please always give a link to this repo
PLotting Capabilities
2D Plots
- plot(v)
- plot(x,y)
- plot(x,y, linespec)
- plot(x1,y1,ls1, x2,y2,ls2, x3,y3,ls3, x4,y4,ls4)
- plot(x, M)
- semilogx(x,y)
- semilogy(x,y)
- loglog(x,y)
3D Plots
- surf(x,y,z)
- surf(x,y,z,lspec)
- surf(x,y,z, palette)
- surf(z, palette)
- contour(x,y,z,palette)
- contour(z,palette)
Animation
- animation_start(delay)
- animation_show()
Multiplot
- multiplot(rows, cols)
Mathematical Utility Functions
- linspace(a,b,n)
- linspace(a,b)
- arange(a,b,dx)
- meshgrid(X,Y, xgv,ygv)
- meshgrid(X,Y, xgv)
Color palette
Nine different color palettes are available. See Ann Schnider gnuplot color palettes and Gnuplotting. These color palettes can be used with:
surf(x,y,z,palette='plt-name')
contour(x,y,z,palette='plt-name')
- set1
- set2
- set3
- palette1
- palette2
- paired
- dark2
- accent
- jet
The ogpf library other features
There are plenty of commands to customise the plots. This includes:
- Plot annotation (e.g. title, xlabel, ylabel, and zlabel)
- Axes setting (e.g. xrange, yrange, zrange, and grid)
call gp%axis([-1.0,+1.0]) ! Sets xrange
call gp%axis([-1.0,+1.0,-2.0,+2.0]) ! Sets both xrange and yrange
call gp%axis([-1.0,+1.0,-2.0,+2.0,-3.0,+3.0]) ! Sets all axis at same time
call gp%xlim([-1.0,+1.0]) ! Sets only the xrange
call gp%ylim([-2.0,+3.0]) ! Sets only the yrange
call gp%zlim([-3.0,+3.0]) ! Sets only the zrange
- Line and marker color and style
- Gnuplot options (e.g. fonts, tics format, frame format,... )
The ogpf options command
The option command is a very powerful command and can be used to customize the gnuplot in many ways. Options can be set by calling the ogpf options. In every call, it is possible to set several options separated by semicolon or options can be set by several calls. Below shows few samples:
- Sample 1
Set the legend (key) at the right bottom of window
call gp%options('set key bottom right')
- Sample 2
Define a new line style
call gp%options('set style line 1 lc rgb "blue" lt 1 lw 2 pt 6 ps 1.5')
- Sample 3
Use several options each uses separate command
call gp%options('set tics')
call gp%options('set tics font ",8"') ! font size for tics
- Sample 4
Set several options at the same time using semicolon as delimiter
call gp%options('unset tics; unset colorbox')
Demo
There is a collection of examples in demo.f90 to show the capabilities of ogpf.
Easy to use
To use ogpf in your project, add the library file to your fortran project (code)
- ogpf.f90 (the main library)
For details see 'demo.f90'
Important Note
To use ogpf on other operating system, you may need to modify the terminal type and fonts in the section of Configuration Parameters. A Makefile has been provided to build the demo from command line.
Example codes
This section shows selected example codes from demo.f90
- Example 1
SUBROUTINE Exmp01
!...............................................................................
!Example 1: A very basic example
!...............................................................................
TYPE(gpf):: gp
INTEGER, PARAMETER:: n=17
Real(wp):: x(n)
Real(wp):: y(n)
! Input data
x=dble([-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8])
y=dble([66,51,38,27,18,11,6,3,2,3,6,11,18,27,38,51,66])
! Annotation: set title, xlabel, ylabel
CALL gp%title('Example 1. A simple xy plot')
CALL gp%xlabel('my x axis ...')
CALL gp%ylabel('my y axis ...')
Call gp%options('set style data linespoints')
!Call Plot to draw a vector against a vector of data
CALL gp%plot(x, y)
END SUBROUTINE Exmp01
Will produce

- Example 04
Plot several data series at the same time
subroutine exmp04
type(gpf):: gp
integer, parameter:: n=50
integer, parameter:: m=65
real(wp):: x(n)
real(wp):: y(n)
real(wp):: xv(m)
real(wp):: yv(m)
real(wp), parameter :: pi=4.d0*atan(1.d0)
! Input data
x=linspace(-pi,pi,n) !linspace is a utility function from module ogpf
y=sin(x)
xv=linspace(0.d0, 2.d0*pi,m)
yv=cos(2.d0*xv)
! This is the maximum number of plot can be drawn at the same time
! If you have more data see, you can plot can be used with matrices!
call gp%title('Example 4. Plot four data sets using gnuplot')
call gp%options('set key top left; set grid')
call gp%plot(x,y, 'title "sin(x)"', &
xv,yv, 'with lp lt 6 title "cos(2x)"', &
xv, 2.d0*yv, 'title "2cos(2x)" lt 7', &
xv, 0.5d0*yv, 'title "0.5cos(2x)" with points pt 8')
! Another example with keyboard arguments
call gp%plot(x1=x,y1=y,x2=xv,y2=yv)
end subroutine exmp04
Will produce

- Example 05
SUBROUTINE Exmp05
!...............................................................................
! Example 5: Use line style and legends
!...............................................................................
TYPE(gpf):: gplot
INTEGER, PARAMETER:: n=50
Real(wp):: x(n)
Real(wp):: ys(n)
Real(wp):: yc(n)
Real(wp):: ysc(n)
Real(wp), PARAMETER :: pi=4.d0*atan(1.d0)
! Input data
x=linspace(-2.d0*pi,2.d0*pi,n) !linspace is a utility function from module Utils
ys=sin(x)
yc=exp(-0.1d0*x)*cos(x)
ysc=sin(x/2.d0)*cos(2.d0*x)
! Annotation, set title, xlabel, ylabel
CALL gplot%title('Example 5. A sample with style and legends')
CALL gplot%xlabel('x, rad')
CALL gplot%ylabel('y, dimensionless')
! Plot to draw three set of data
CALL gplot%plot(x,ys,'title "sin" with lines lt 5 lc rgb "#0008B0"', &
x,yc,'title "cos" with points lt 6 lc rgb "#FF1100"', &
x,ysc,'title "sin(x/2)cos(2x)" with lp lt 7 lc rgb "#00AA04"' )
END SUBROUTINE Exmp05
Will produce

- Example 06
subroutine exmp06
type(gpf):: gplot
