add_scatter Subroutine

private subroutine add_scatter(me, x, y, label, s, marker, color, alpha, xlim, ylim, xscale, yscale, edgecolors, linewidths, istat)

Add a scatter plot.

Example

  call plt%add_scatter(x, y, label='data', s=50, color='blue', &
                       marker='o', alpha=0.6_wp)

Type Bound

pyplot

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

integer, intent(in), optional :: s

marker size (area in points^2)

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

marker style (default: 'o')

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

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

real(kind=wp), intent(in), optional :: alpha

transparency (0.0 to 1.0)

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'

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

edge color of markers

integer, intent(in), optional :: linewidths

line width of marker edges

integer, intent(out), optional :: istat

status output (0 means no problems)


Calls

proc~~add_scatter~~CallsGraph proc~add_scatter pyplot%add_scatter proc~add_str pyplot%add_str proc~add_scatter->proc~add_str proc~optional_int_to_string optional_int_to_string proc~add_scatter->proc~optional_int_to_string proc~real_to_string real_to_string proc~add_scatter->proc~real_to_string proc~vec_to_string vec_to_string proc~add_scatter->proc~vec_to_string proc~integer_to_string integer_to_string proc~optional_int_to_string->proc~integer_to_string

Source Code

    subroutine add_scatter(me, x, y, label, s, marker, color, alpha, &
                           xlim, ylim, xscale, yscale, edgecolors, linewidths, istat)

    class(pyplot),          intent(inout)         :: me           !! pyplot handler
    real(wp), dimension(:), intent(in)            :: x            !! x values
    real(wp), dimension(:), intent(in)            :: y            !! y values
    character(len=*),       intent(in)            :: label        !! plot label
    integer,                intent(in), optional  :: s            !! marker size (area in points^2)
    character(len=*),       intent(in), optional  :: marker       !! marker style (default: 'o')
    real(wp), dimension(:), intent(in), optional  :: color        !! RGB color tuple [0-1,0-1,0-1]
    real(wp),               intent(in), optional  :: alpha        !! transparency (0.0 to 1.0)
    real(wp), dimension(2), intent(in), optional  :: xlim         !! x-axis range
    real(wp), dimension(2), intent(in), optional  :: ylim         !! y-axis range
    character(len=*),       intent(in), optional  :: xscale       !! example: 'linear' (default), 'log'
    character(len=*),       intent(in), optional  :: yscale       !! example: 'linear' (default), 'log'
    character(len=*),       intent(in), optional  :: edgecolors   !! edge color of markers
    integer,                intent(in), optional  :: linewidths   !! line width of marker edges
    integer,                intent(out), optional :: istat        !! status output (0 means no problems)

    character(len=:), allocatable :: arg_str      !! the arguments to pass to `scatter`
    character(len=:), allocatable :: xstr         !! x values stringified
    character(len=:), allocatable :: ystr         !! y values stringified
    character(len=:), allocatable :: xlimstr      !! xlim values stringified
    character(len=:), allocatable :: ylimstr      !! ylim values stringified
    character(len=:), allocatable :: color_str    !! color values stringified
    character(len=:), allocatable :: alpha_str    !! alpha value stringified
    character(len=max_int_len)    :: size_str     !! marker size
    character(len=max_int_len)    :: linewidth_str !! line width
    character(len=*), parameter   :: xname = 'x'  !! x variable name for script
    character(len=*), parameter   :: yname = 'y'  !! y variable name for script

    if (allocated(me%str)) then

        if (present(istat)) istat = 0

        !axis limits (optional):
        if (present(xlim)) call vec_to_string(xlim, me%real_fmt, xlimstr, me%use_numpy)
        if (present(ylim)) call vec_to_string(ylim, me%real_fmt, ylimstr, me%use_numpy)

        !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)

        !get optional inputs (if not present, set default value):
        call optional_int_to_string(s, size_str, '20')
        call optional_int_to_string(linewidths, linewidth_str, '1')

        !write the arrays:
        call me%add_str(trim(xname)//' = '//xstr)
        call me%add_str(trim(yname)//' = '//ystr)
        call me%add_str('')

        !main arguments for scatter:
        arg_str = trim(xname)//','//&
                  trim(yname)//','//&
                  's='//trim(adjustl(size_str))//','//&
                  'label='//trim(me%raw_str_token)//'"'//trim(label)//'"'

        ! optional arguments:
        if (present(marker)) then
            arg_str = arg_str//',marker='//trim(me%raw_str_token)//'"'//trim(marker)//'"'
        end if

        if (present(color)) then
            if (size(color)<=3) then
                call vec_to_string(color(1:3), '*', color_str, use_numpy=.false., is_tuple=.true.)
                arg_str = arg_str//',color='//trim(color_str)
            end if
        end if

        if (present(alpha)) then
            call real_to_string(alpha, me%real_fmt, alpha_str)
            arg_str = arg_str//',alpha='//alpha_str
        end if

        if (present(edgecolors)) then
            arg_str = arg_str//',edgecolors='//trim(me%raw_str_token)//'"'//trim(edgecolors)//'"'
        end if

        if (present(linewidths)) then
            arg_str = arg_str//',linewidths='//trim(adjustl(linewidth_str))
        end if

        !write the scatter statement:
        call me%add_str('ax.scatter('//arg_str//')')

        !axis limits:
        if (allocated(xlimstr)) call me%add_str('ax.set_xlim('//xlimstr//')')
        if (allocated(ylimstr)) call me%add_str('ax.set_ylim('//ylimstr//')')

        !axis scales:
        if (present(xscale)) call me%add_str('ax.set_xscale('//trim(me%raw_str_token)//'"'//xscale//'")')
        if (present(yscale)) call me%add_str('ax.set_yscale('//trim(me%raw_str_token)//'"'//yscale//'")')

        call me%add_str('')

    else
        if (present(istat)) istat = -1
        write(error_unit,'(A)') 'Error in add_scatter: pyplot class not properly initialized.'
    end if

    end subroutine add_scatter