initialize Subroutine

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)

Initialize a plot

Type Bound

pyplot

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


Calls

proc~~initialize~~CallsGraph proc~initialize pyplot%initialize proc~add_str pyplot%add_str proc~initialize->proc~add_str proc~destroy pyplot%destroy proc~initialize->proc~destroy proc~integer_to_string integer_to_string proc~initialize->proc~integer_to_string proc~optional_int_to_string optional_int_to_string proc~initialize->proc~optional_int_to_string proc~optional_int_to_string->proc~integer_to_string

Source Code

    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)

    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, dimension(2), intent(in), optional :: 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

    character(len=max_int_len)  :: width_str             !! figure width dummy string
    character(len=max_int_len)  :: height_str            !! figure height dummy string
    character(len=max_int_len)  :: font_size_str         !! font size dummy string
    character(len=max_int_len)  :: axes_labelsize_str    !! size of axis labels dummy string
    character(len=max_int_len)  :: xtick_labelsize_str   !! size of x axis tick labels dummy string
    character(len=max_int_len)  :: ytick_labelsize_str   !! size of x axis tick labels dummy string
    character(len=max_int_len)  :: ztick_labelsize_str   !! size of z axis tick labels dummy string
    character(len=max_int_len)  :: legend_fontsize_str   !! size of legend font dummy string
    character(len=:),allocatable :: python_fig_func      !! Python's function for creating a new Figure instance

    character(len=*), parameter :: default_font_size_str = '10' !! the default font size for plots

    call me%destroy()

    if (present(raw_strings)) then
        if (raw_strings) me%raw_str_token = 'r'
    end if

    if (present(legend)) then
        me%show_legend = legend
    else
        me%show_legend = .false.
    end if
    if (present(use_numpy)) then
        me%use_numpy = use_numpy
    else
        me%use_numpy = .true.
    end if
    if (present(use_oo_api)) then
        me%use_oo_api = use_oo_api
    else
        me%use_oo_api = .false.
    end if
    if (present(figsize)) then
        call integer_to_string(figsize(1), width_str)
        call integer_to_string(figsize(2), height_str)
    end if
    if (present(mplot3d)) then
        me%mplot3d = mplot3d
    else
        me%mplot3d = .false.
    end if
    if (present(polar)) then
        me%polar = polar
    else
        me%polar = .false.
    end if
    if (present(axis_equal)) then
        me%axis_equal = axis_equal
    else
        me%axis_equal = .false.
    end if
    if (present(real_fmt)) then
        me%real_fmt = trim(adjustl(real_fmt))
    else
        me%real_fmt = real_fmt_default
    end if
    if (present(tight_layout)) then
        me%tight_layout = tight_layout
    else
        me%tight_layout = .false.
    end if
    if (present(usetex)) then
        me%usetex = usetex
    else
        me%usetex = .false.
    end if
    if (present(xaxis_date_fmt)) then
        me%xaxis_date_fmt = xaxis_date_fmt
    else
        if (allocated(me%xaxis_date_fmt)) deallocate(me%xaxis_date_fmt)
    end if
    if (present(yaxis_date_fmt)) then
        me%yaxis_date_fmt = yaxis_date_fmt
    else
        if (allocated(me%yaxis_date_fmt)) deallocate(me%yaxis_date_fmt)
    end if

    call optional_int_to_string(font_size, font_size_str, default_font_size_str)
    call optional_int_to_string(axes_labelsize, axes_labelsize_str, default_font_size_str)
    call optional_int_to_string(xtick_labelsize, xtick_labelsize_str, default_font_size_str)
    call optional_int_to_string(ytick_labelsize, ytick_labelsize_str, default_font_size_str)
    call optional_int_to_string(ztick_labelsize, ztick_labelsize_str, default_font_size_str)
    call optional_int_to_string(legend_fontsize, legend_fontsize_str, default_font_size_str)

    me%str = ''

    call me%add_str('#!/usr/bin/env python')
    call me%add_str('')

    call me%add_str('import matplotlib')
    if (me%use_oo_api) then
        call me%add_str('from matplotlib.figure import Figure')
        call me%add_str('from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas')
    else
        call me%add_str('import matplotlib.pyplot as plt')
    endif
    if (me%mplot3d) call me%add_str('from mpl_toolkits.mplot3d import Axes3D')
    if (me%use_numpy) call me%add_str('import numpy as np')
    call me%add_str('')

    call me%add_str('matplotlib.rcParams["font.family"] = "Serif"')
    call me%add_str('matplotlib.rcParams["font.size"] = '//trim(font_size_str))
    call me%add_str('matplotlib.rcParams["axes.labelsize"] = '//trim(axes_labelsize_str))
    call me%add_str('matplotlib.rcParams["xtick.labelsize"] = '//trim(xtick_labelsize_str))
    call me%add_str('matplotlib.rcParams["ytick.labelsize"] = '//trim(ytick_labelsize_str))
    call me%add_str('matplotlib.rcParams["legend.fontsize"] = '//trim(legend_fontsize_str))
    if (me%usetex) call me%add_str('matplotlib.rcParams["text.usetex"] = True')

    call me%add_str('')

    if (me%use_oo_api) then
        python_fig_func = 'Figure'
    else
        python_fig_func = 'plt.figure'
    endif
    if (present(figsize)) then  !if specifying the figure size
        call me%add_str('fig = '//python_fig_func//'(figsize=('//trim(width_str)//','//trim(height_str)//'),facecolor="white")')
    else
        call me%add_str('fig = '//python_fig_func//'(facecolor="white")')
    end if

    if (me%mplot3d) then
        call me%add_str('ax = fig.add_subplot(1, 1, 1, projection=''3d'')')
    elseif (me%polar) then
        call me%add_str('ax = fig.add_subplot(1, 1, 1, projection=''polar'')')
    else
        call me%add_str('ax = fig.add_subplot(1, 1, 1)')
    end if

    if (present(grid)) then
        if (grid) call me%add_str('ax.grid()')
    end if

    if (present(axisbelow)) then
        me%axisbelow = axisbelow
    else
        me%axisbelow = .true. ! default
    end if
    if (me%axisbelow) call me%add_str('ax.set_axisbelow(True)')

    if (present(xlabel)) call me%add_str('ax.set_xlabel('//trim(me%raw_str_token)//'"'//trim(xlabel)//'")')
    if (present(ylabel)) call me%add_str('ax.set_ylabel('//trim(me%raw_str_token)//'"'//trim(ylabel)//'")')
    if (present(zlabel)) call me%add_str('ax.set_zlabel('//trim(me%raw_str_token)//'"'//trim(zlabel)//'")')
    if (present(title))  call me%add_str('ax.set_title('//trim(me%raw_str_token)//'"' //trim(title) //'")')

    call me%add_str('')

    end subroutine initialize