pyplot_module Module

For making simple x-y plots from Fortran. It works by generating a Python script and executing it.

See also

Note

The default real kind (wp) can be changed using optional preprocessor flags. This library was built with real kind: real(kind=real64) [8 bytes]


Uses

  • module~~pyplot_module~~UsesGraph module~pyplot_module pyplot_module iso_fortran_env iso_fortran_env module~pyplot_module->iso_fortran_env

Variables

Type Visibility Attributes Name Initial
integer, public, parameter :: pyplot_wp = real64

real kind used by this module [8 bytes]

integer, private, parameter :: wp = pyplot_wp

local copy of pyplot_wp with a shorter name

character(len=*), private, parameter :: tmp_file = 'pyplot_module_temp_1234567890.py'

Default name of the temporary file (this can also be user-specified).

character(len=*), private, parameter :: python_exe = 'python'

The python executable name.

character(len=*), private, parameter :: int_fmt = '(I10)'

integer format string

integer, private, parameter :: max_int_len = 10

max string length for integers

character(len=*), private, parameter :: real_fmt_default = '(E30.16)'

default real number format string

integer, private, parameter :: max_real_len = 60

max string length for reals


Derived Types

type, public ::  pyplot

The main pyplot class.

Components

Type Visibility Attributes Name Initial
character(len=:), private, allocatable :: str

string buffer

character(len=1), private :: raw_str_token = ' '

will be 'r' if using raw strings

logical, private :: show_legend = .false.

show legend into plot

logical, private :: use_numpy = .true.

use numpy python module

logical, private :: use_oo_api = .false.

use OO interface of matplotlib (incopatible with showfig subroutine)

logical, private :: mplot3d = .false.

it is a 3d plot

logical, private :: polar = .false.

it is a polar plot

logical, private :: axis_equal = .false.

equal scale on each axis

logical, private :: axisbelow = .true.

axis below other chart elements

logical, private :: tight_layout = .false.

tight layout option

logical, private :: usetex = .false.

enable LaTeX

character(len=:), private, allocatable :: xaxis_date_fmt

date format for the x-axis. Example: "%m/%d/%y %H:%M:%S"

character(len=:), private, allocatable :: yaxis_date_fmt

date format for the y-axis. Example: "%m/%d/%y %H:%M:%S"

character(len=:), private, allocatable :: real_fmt

real number formatting

Type-Bound Procedures

procedure, public :: initialize ../../

initialize pyplot instance

procedure, public :: add_plot ../../

add a 2d plot to pyplot instance

procedure, public :: add_errorbar ../../

add a 2d error bar plot to pyplot instance

procedure, public :: add_3d_plot ../../

add a 3d plot to pyplot instance

procedure, public :: add_sphere ../../

add a 3d sphere to pyplot instance

procedure, public :: add_contour ../../

add a contour plot to pyplot instance

procedure, public :: plot_wireframe ../../

add a wireframe plot to pyplot instance

procedure, public :: plot_surface ../../

add a surface plot to pyplot instance

procedure, public :: add_bar ../../

add a barplot to pyplot instance

procedure, public :: add_imshow ../../

add an image plot (using imshow)

procedure, public :: add_hist ../../

add a histogram plot to pyplot instance

procedure, public :: savefig ../../

save plots of pyplot instance

procedure, public :: showfig ../../

show plots of pyplot instance

procedure, public :: destroy ../../

destroy pyplot instance

procedure, public :: execute ../../

execute pyplot commands

procedure, public :: add_str ../../

add string to pytplot instance buffer

procedure, public :: finish_ops ../../

some final ops before saving


Subroutines

private subroutine destroy(me)

Author
Jacob Williams

Destructor.

Arguments

Type IntentOptional Attributes Name
class(pyplot), intent(inout) :: me

pyplot handler

private subroutine add_str(me, str)

Author
Jacob Williams

Add a string to the buffer.

Arguments

Type IntentOptional Attributes Name
class(pyplot), intent(inout) :: me

pyplot handler

character(len=*), intent(in) :: str

str to be added to pyplot handler buffer

private subroutine initialize(me, grid, xlabel, ylabel, zlabel, title, legend, use_numpy, figsize, font_size, axes_labelsize, xtick_labelsize, ytick_labelsize, ztick_labelsize, legend_fontsize, mplot3d, axis_equal, polar, real_fmt, use_oo_api, axisbelow, tight_layout, raw_strings, usetex, xaxis_date_fmt, yaxis_date_fmt)

Author
Jacob Williams

Initialize a plot

Arguments

Type IntentOptional Attributes Name
class(pyplot), intent(inout) :: me

pyplot handler

logical, intent(in), optional :: grid

activate grid drawing

character(len=*), intent(in), optional :: xlabel

label of x axis

character(len=*), intent(in), optional :: ylabel

label of y axis

character(len=*), intent(in), optional :: zlabel

label of z axis

character(len=*), intent(in), optional :: title

plot title

logical, intent(in), optional :: legend

plot legend

logical, intent(in), optional :: use_numpy

activate usage of numpy python module

integer, intent(in), optional, dimension(2) :: figsize

dimension of the figure

integer, intent(in), optional :: font_size

font size

integer, intent(in), optional :: axes_labelsize

size of axis labels

integer, intent(in), optional :: xtick_labelsize

size of x axis tick lables

integer, intent(in), optional :: ytick_labelsize

size of y axis tick lables

integer, intent(in), optional :: ztick_labelsize

size of z axis tick lables

integer, intent(in), optional :: legend_fontsize

size of legend font

logical, intent(in), optional :: mplot3d

set true for 3d plots (cannot use with polar)

logical, intent(in), optional :: axis_equal

set true for axis = 'equal'

logical, intent(in), optional :: polar

set true for polar plots (cannot use with mplot3d)

character(len=*), intent(in), optional :: real_fmt

format string for real numbers (examples: '(E30.16)' [default], '*')

logical, intent(in), optional :: use_oo_api

avoid matplotlib's GUI by using the OO interface (cannot use with showfig)

logical, intent(in), optional :: axisbelow

to put the grid lines below the other chart elements [default is true]

logical, intent(in), optional :: tight_layout

enable tight layout [default is false]

logical, intent(in), optional :: raw_strings

if True, all strings sent to Python are treated as raw strings (e.g., r'str'). Default is False.

logical, intent(in), optional :: usetex

if True, enable LaTeX. (default if false)

character(len=*), intent(in), optional :: xaxis_date_fmt

if present, used to set the date format for the x-axis

character(len=*), intent(in), optional :: yaxis_date_fmt

if present, used to set the date format for the y-axis

private subroutine add_plot(me, x, y, label, linestyle, markersize, linewidth, xlim, ylim, xscale, yscale, color, istat)

Author
Jacob Williams

Add an x,y plot.

Arguments

Type IntentOptional Attributes Name
class(pyplot), intent(inout) :: me

pyplot handler

real(kind=wp), intent(in), dimension(:) :: x

x values

real(kind=wp), intent(in), dimension(:) :: y

y values

character(len=*), intent(in) :: label

plot label

character(len=*), intent(in) :: linestyle

style of the plot line

integer, intent(in), optional :: markersize

size of the plot markers

integer, intent(in), optional :: linewidth

width of the plot line

real(kind=wp), intent(in), optional, dimension(2) :: xlim

x-axis range

real(kind=wp), intent(in), optional, dimension(2) :: ylim

y-axis range

character(len=*), intent(in), optional :: xscale

example: 'linear' (default), 'log'

character(len=*), intent(in), optional :: yscale

example: 'linear' (default), 'log'

real(kind=wp), intent(in), optional, dimension(:) :: color

RGB color tuple [0-1,0-1,0-1]

integer, intent(out), optional :: istat

status output (0 means no problems)

private subroutine add_hist(me, x, label, xlim, ylim, xscale, yscale, bins, normed, cumulative, istat)

Author
Jimmy Leta

Add a histogram plot.

Arguments

Type IntentOptional Attributes Name
class(pyplot), intent(inout) :: me

pyplot handler

real(kind=wp), intent(in), dimension(:) :: x

array of data

character(len=*), intent(in) :: label

plot label

real(kind=wp), intent(in), optional, dimension(2) :: xlim

x-axis range

real(kind=wp), intent(in), optional, dimension(2) :: ylim

y-axis range

character(len=*), intent(in), optional :: xscale

example: 'linear' (default), 'log'

character(len=*), intent(in), optional :: yscale

example: 'linear' (default), 'log'

integer, intent(in), optional :: bins

number of bins

logical, intent(in), optional :: normed

boolean flag that determines whether bin counts are normalized [NO LONGER USED]

logical, intent(in), optional :: cumulative

boolean flag that determines whether histogram represents the cumulative density of dataset

integer, intent(out), optional :: istat

status output (0 means no problems)

private subroutine add_contour(me, x, y, z, linestyle, linewidth, levels, color, filled, cmap, colorbar, istat)

Author
Jacob Williams

Add a contour plot.

Read more…

Arguments

Type IntentOptional Attributes Name
class(pyplot), intent(inout) :: me

pyplot handler

real(kind=wp), intent(in), dimension(:) :: x

x values

real(kind=wp), intent(in), dimension(:) :: y

y values

real(kind=wp), intent(in), dimension(:,:) :: z

z values (a matrix)

character(len=*), intent(in) :: linestyle

style of the plot line

integer, intent(in), optional :: linewidth

width of the plot line [only used when filled=False]

real(kind=wp), intent(in), optional, dimension(:) :: levels

contour levels to plot

character(len=*), intent(in), optional :: color

color of the contour line

logical, intent(in), optional :: filled

use filled control (default=False)

character(len=*), intent(in), optional :: cmap

colormap if filled=True (examples: 'jet', 'bone')

logical, intent(in), optional :: colorbar

add a colorbar (default=False)

integer, intent(out), optional :: istat

status output (0 means no problems)

private subroutine plot_surface(me, x, y, z, label, linestyle, linewidth, levels, color, cmap, colorbar, antialiased, istat)

Author
Jacob Williams

Add a surface plot.

Read more…

Arguments

Type IntentOptional Attributes Name
class(pyplot), intent(inout) :: me

pyplot handler

real(kind=wp), intent(in), dimension(:) :: x

x values

real(kind=wp), intent(in), dimension(:) :: y

y values

real(kind=wp), intent(in), dimension(:,:) :: z

z values (a matrix)

character(len=*), intent(in) :: label

plot label

character(len=*), intent(in) :: linestyle

style of the plot line

integer, intent(in), optional :: linewidth

width of the plot line

real(kind=wp), intent(in), optional, dimension(:) :: levels

contour levels to plot

character(len=*), intent(in), optional :: color

Color of the surface patches

character(len=*), intent(in), optional :: cmap

colormap if filled=True (examples: 'jet', 'bone')

logical, intent(in), optional :: colorbar

add a colorbar (default=False)

logical, intent(in), optional :: antialiased

The surface is made opaque by using antialiased=False

integer, intent(out), optional :: istat

status output (0 means no problems)

private subroutine plot_wireframe(me, x, y, z, label, linestyle, linewidth, levels, color, cmap, colorbar, antialiased, istat)

Author
Jacob Williams

Add a wireframe plot.

Read more…

Arguments

Type IntentOptional Attributes Name
class(pyplot), intent(inout) :: me

pyplot handler

real(kind=wp), intent(in), dimension(:) :: x

x values

real(kind=wp), intent(in), dimension(:) :: y

y values

real(kind=wp), intent(in), dimension(:,:) :: z

z values (a matrix)

character(len=*), intent(in) :: label

plot label

character(len=*), intent(in) :: linestyle

style of the plot line

integer, intent(in), optional :: linewidth

width of the plot line

real(kind=wp), intent(in), optional, dimension(:) :: levels

contour levels to plot

character(len=*), intent(in), optional :: color

Color of the surface patches

character(len=*), intent(in), optional :: cmap

colormap if filled=True (examples: 'jet', 'bone')

logical, intent(in), optional :: colorbar

add a colorbar (default=False)

logical, intent(in), optional :: antialiased

The surface is made opaque by using antialiased=False

integer, intent(out), optional :: istat

status output (0 means no problems)

private subroutine add_3d_plot(me, x, y, z, label, linestyle, markersize, linewidth, istat)

Author
Jacob Williams

Add a 3D x,y,z plot.

Read more…

Arguments

Type IntentOptional Attributes Name
class(pyplot), intent(inout) :: me

pyplot handler

real(kind=wp), intent(in), dimension(:) :: x

x values

real(kind=wp), intent(in), dimension(:) :: y

y values

real(kind=wp), intent(in), dimension(:) :: z

z values

character(len=*), intent(in) :: label

plot label

character(len=*), intent(in) :: linestyle

style of the plot line

integer, intent(in), optional :: markersize

size of the plot markers

integer, intent(in), optional :: linewidth

width of the plot line

integer, intent(out), optional :: istat

status output (0 means no problems)

private subroutine add_sphere(me, r, xc, yc, zc, n_facets, linewidth, antialiased, color, istat)

Author
Jacob Williams

Add a sphere to a 3D x,y,z plot.

Read more…

Arguments

Type IntentOptional Attributes Name
class(pyplot), intent(inout) :: me

pyplot handler

real(kind=wp), intent(in) :: r

radius of the sphere

real(kind=wp), intent(in) :: xc

x value of sphere center

real(kind=wp), intent(in) :: yc

y value of sphere center

real(kind=wp), intent(in) :: zc

z value of sphere center

integer, intent(in), optional :: n_facets

[default is 100]

integer, intent(in), optional :: linewidth

line width

logical, intent(in), optional :: antialiased

enabled anti-aliasing

character(len=*), intent(in), optional :: color

color of the contour line

integer, intent(out), optional :: istat

status output (0 means no problems)

private subroutine add_bar(me, x, height, label, width, bottom, color, yerr, align, xlim, ylim, xscale, yscale, istat)

Author
Jacob Williams

Add a bar plot.

Arguments

Type IntentOptional Attributes Name
class(pyplot), intent(inout) :: me

pyplot handler

real(kind=wp), intent(in), dimension(:) :: x

x bar values

real(kind=wp), intent(in), dimension(:) :: height

height bar values

character(len=*), intent(in) :: label

plot label

real(kind=wp), intent(in), optional, dimension(:) :: width

width values

real(kind=wp), intent(in), optional, dimension(:) :: bottom

bottom values

character(len=*), intent(in), optional :: color

plot color

real(kind=wp), intent(in), optional, dimension(:) :: yerr

yerr values

character(len=*), intent(in), optional :: align

default: 'center'

real(kind=wp), intent(in), optional, dimension(2) :: xlim

x-axis range

real(kind=wp), intent(in), optional, dimension(2) :: ylim

y-axis range

character(len=*), intent(in), optional :: xscale

example: 'linear' (default), 'log'

character(len=*), intent(in), optional :: yscale

example: 'linear' (default), 'log'

integer, intent(out), optional :: istat

status output (0 means no problems)

private subroutine add_imshow(me, x, xlim, ylim, istat)

Add an image plot using imshow.

Read more…

Arguments

Type IntentOptional Attributes Name
class(pyplot), intent(inout) :: me

pyplot handler

real(kind=wp), intent(in), dimension(:,:) :: x

x values

real(kind=wp), intent(in), optional, dimension(2) :: xlim

x-axis range

real(kind=wp), intent(in), optional, dimension(2) :: ylim

y-axis range

integer, intent(out), optional :: istat

status output (0 means no problems)

private subroutine add_errorbar(me, x, y, label, linestyle, xerr, yerr, markersize, linewidth, xlim, ylim, xscale, yscale, color, istat)

Author
Alexander Sandrock

Add an x,y plot with errorbars.

Arguments

Type IntentOptional Attributes Name
class(pyplot), intent(inout) :: me

pyplot handler

real(kind=wp), intent(in), dimension(:) :: x

x values

real(kind=wp), intent(in), dimension(:) :: y

y values

character(len=*), intent(in) :: label

plot label

character(len=*), intent(in) :: linestyle

style of the plot line

real(kind=wp), intent(in), optional, dimension(:) :: xerr

x errorbar sizes

real(kind=wp), intent(in), optional, dimension(:) :: yerr

y errorbar sizes

integer, intent(in), optional :: markersize

size of the plot markers

integer, intent(in), optional :: linewidth

width of the plot line

real(kind=wp), intent(in), optional, dimension(2) :: xlim

x-axis range

real(kind=wp), intent(in), optional, dimension(2) :: ylim

y-axis range

character(len=*), intent(in), optional :: xscale

example: 'linear' (default), 'log'

character(len=*), intent(in), optional :: yscale

example: 'linear' (default), 'log'

real(kind=wp), intent(in), optional, dimension(:) :: color

RGB color tuple [0-1,0-1,0-1]

integer, intent(out), optional :: istat

status output (0 means no problems)

private subroutine optional_int_to_string(int_value, string_value, default_value)

Author
Jacob Williams

Integer to string, specifying the default value if the optional argument is not present.

Arguments

Type IntentOptional Attributes Name
integer, intent(in), optional :: int_value

integer value

character(len=*), intent(out) :: string_value

integer value stringified

character(len=*), intent(in) :: default_value

default integer value

private subroutine optional_logical_to_string(logical_value, string_value, default_value)

Author
Jacob Williams

Logical to string, specifying the default value if the optional argument is not present.

Arguments

Type IntentOptional Attributes Name
logical, intent(in), optional :: logical_value
character(len=:), intent(out), allocatable :: string_value

integer value stringified

character(len=*), intent(in) :: default_value

default integer value

private subroutine integer_to_string(i, s)

Author
Jacob Williams

Integer to string conversion.

Arguments

Type IntentOptional Attributes Name
integer, intent(in), optional :: i

integer value

character(len=*), intent(out) :: s

integer value stringified

private subroutine real_to_string(v, fmt, str)

Author
Jacob Williams

Real scalar to string.

Arguments

Type IntentOptional Attributes Name
real(kind=wp), intent(in) :: v

real values

character(len=*), intent(in) :: fmt

real format string

character(len=:), intent(out), allocatable :: str

real values stringified

private subroutine vec_to_string(v, fmt, str, use_numpy, is_tuple)

Author
Jacob Williams

Real vector to string.

Arguments

Type IntentOptional Attributes Name
real(kind=wp), intent(in), dimension(:) :: v

real values

character(len=*), intent(in) :: fmt

real format string

character(len=:), intent(out), allocatable :: str

real values stringified

logical, intent(in) :: use_numpy

activate numpy python module usage

logical, intent(in), optional :: is_tuple

if true [default], use '()', if false use '[]'

private subroutine matrix_to_string(v, fmt, str, use_numpy)

Author
Jacob Williams

Real matrix (rank 2) to string.

Arguments

Type IntentOptional Attributes Name
real(kind=wp), intent(in), dimension(:,:) :: v

real values

character(len=*), intent(in) :: fmt

real format string

character(len=:), intent(out), allocatable :: str

real values stringified

logical, intent(in) :: use_numpy

activate numpy python module usage

private subroutine execute(me, pyfile, istat, python)

Author
Jacob Williams
Date
8/16/2015

Write the buffer to a file, and then execute it with Python.

Read more…

Arguments

Type IntentOptional Attributes Name
class(pyplot), intent(inout) :: me

pytplot handler

character(len=*), intent(in), optional :: pyfile

name of the python script to generate

integer, intent(out), optional :: istat

status output (0 means no problems)

character(len=*), intent(in), optional :: python

python executable to use. (by default, this is 'python')

private subroutine finish_ops(me)

Author
Jacob Williams

Some final things to add before saving or showing the figure.

Arguments

Type IntentOptional Attributes Name
class(pyplot), intent(inout) :: me

pyplot handler

private subroutine savefig(me, figfile, pyfile, dpi, transparent, facecolor, edgecolor, orientation, istat, python)

Author
Jacob Williams

Save the figure.

Read more…

Arguments

Type IntentOptional Attributes Name
class(pyplot), intent(inout) :: me

pyplot handler

character(len=*), intent(in) :: figfile

file name for the figure

character(len=*), intent(in), optional :: pyfile

name of the Python script to generate

character(len=*), intent(in), optional :: dpi

resolution of the figure for png [note this is a string]

logical, intent(in), optional :: transparent

transparent background (T/F)

character(len=*), intent(in), optional :: facecolor

the colors of the figure rectangle

character(len=*), intent(in), optional :: edgecolor

the colors of the figure rectangle

character(len=*), intent(in), optional :: orientation

'landscape' or 'portrait'

integer, intent(out), optional :: istat

status output (0 means no problems)

character(len=*), intent(in), optional :: python

python executable to use. (by default, this is 'python')

private subroutine showfig(me, pyfile, istat, python)

Author
Johannes Rieke
Date
6/16/2017

Shows the figure.

Arguments

Type IntentOptional Attributes Name
class(pyplot), intent(inout) :: me

pyplot handler

character(len=*), intent(in), optional :: pyfile

name of the Python script to generate

integer, intent(out), optional :: istat

status output (0 means no problems)

character(len=*), intent(in), optional :: python

python executable to use. (by default, this is 'python')