subroutine add_contour(me, x, y, z, label, linestyle, linewidth, levels, color, filled, cmap)
class(pyplot), intent (inout) :: me !! pyplot handler
real(wp),dimension(:), intent (in) :: x !! x values
real(wp),dimension(:), intent (in) :: y !! y values
real(wp),dimension(:,:), intent (in) :: 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(wp),dimension(:), intent (in), optional :: 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')
character(len=:), allocatable :: xstr !! x values strinfied
character(len=:), allocatable :: ystr !! y values strinfied
character(len=:), allocatable :: zstr !! z values strinfied
character(len=:), allocatable :: levelstr !! levels vector strinfied
character(len=max_int_len) :: iline !! actual line width
character(len=*), parameter :: xname = 'x' !! x variable name for script
character(len=*), parameter :: yname = 'y' !! y variable name for script
character(len=*), parameter :: zname = 'z' !! z variable name for script
character(len=*), parameter :: xname_ = 'X' !! X variable name for contour
character(len=*), parameter :: yname_ = 'Y' !! Y variable name for contour
character(len=*), parameter :: zname_ = 'Z' !! Z variable name for contour
character(len=:), allocatable :: extras !! optional stuff
character(len=:), allocatable :: contourfunc !! 'contour' or 'contourf'
if (allocated(me%str)) then
!convert the arrays to strings:
call vec_to_string(x, me%real_fmt, xstr, me%use_numpy)
call vec_to_string(y, me%real_fmt, ystr, me%use_numpy)
call matrix_to_string(z, me%real_fmt, zstr, me%use_numpy)
if (present(levels)) call vec_to_string(levels, me%real_fmt, levelstr, me%use_numpy)
!get optional inputs (if not present, set default value):
call optional_int_to_string(linewidth, iline, '3')
!write the arrays:
call me%add_str(trim(xname)//' = '//xstr)
call me%add_str(trim(yname)//' = '//ystr)
call me%add_str(trim(zname)//' = '//zstr)
call me%add_str('')
!convert inputs for contour plotting:
call me%add_str(yname_//', '//xname_//' = np.meshgrid('//trim(xname)//', '//trim(yname)//')')
call me%add_str(zname_//' = '//zname)
!optional arguments:
extras = ''
if (present(levels)) extras = extras//','//'levels='//levelstr
if (present(color)) extras = extras//','//'colors="'//color//'"'
if (present(linewidth)) extras = extras//','//'linewidths='//trim(adjustl(iline))
if (present(cmap)) extras = extras//','//'cmap="'//cmap//'"'
!filled or regular:
contourfunc = 'contour' !default
if (present(filled)) then
if (filled) contourfunc = 'contourf' !filled contour
end if
!write the plot statement:
call me%add_str('CS = ax.'//contourfunc//'('//xname_//','//yname_//','//zname_//','//&
'label="'//trim(label)//'",'//&
'linestyles="'//trim(adjustl(linestyle))//'"'//&
extras//')')
call me%add_str('ax.clabel(CS, fontsize=9, inline=1)')
call me%add_str('')
else
error stop 'Error in add_plot: pyplot class not properly initialized.'
end if
end subroutine add_contour