Skip to content

Commit 63e3808

Browse files
Merge pull request #27 from asandrock/master
Wrote subroutine add_errorbar
2 parents 2614fbc + b992bfa commit 63e3808

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed

src/pyplot_module.f90

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ module pyplot_module
5353
procedure, public :: initialize !! initialize pyplot instance
5454

5555
procedure, public :: add_plot !! add a 2d plot to pyplot instance
56+
procedure, public :: add_errorbar !! add a 2d error bar plot to pyplot instance
5657
procedure, public :: add_3d_plot !! add a 3d plot to pyplot instance
5758
procedure, public :: add_sphere !! add a 3d sphere to pyplot instance
5859
procedure, public :: add_contour !! add a contour plot to pyplot instance
@@ -1315,6 +1316,114 @@ subroutine showfig(me, pyfile, istat)
13151316
end subroutine showfig
13161317
!*****************************************************************************************
13171318

1319+
!*****************************************************************************************
1320+
!> author: Alexander Sandrock
1321+
!
1322+
! Add an x,y plot.
1323+
1324+
subroutine add_errorbar(me, x, y, label, linestyle, xerr, yerr, markersize, linewidth, xlim, ylim, xscale, yscale, color, istat)
1325+
1326+
class(pyplot), intent (inout) :: me !! pyplot handler
1327+
real(wp), dimension(:), intent (in) :: x !! x values
1328+
real(wp), dimension(:), intent (in) :: y !! y values
1329+
character(len=*), intent (in) :: label !! plot label
1330+
character(len=*), intent (in) :: linestyle !! style of the plot line
1331+
real(wp), dimension(:), intent (in), optional :: xerr !! x errorbar sizes
1332+
real(wp), dimension(:), intent (in), optional :: yerr !! y errorbar sizes
1333+
integer, intent (in), optional :: markersize !! size of the plot markers
1334+
integer, intent (in), optional :: linewidth !! width of the plot line
1335+
real(wp),dimension(2), intent (in), optional :: xlim !! x-axis range
1336+
real(wp),dimension(2), intent (in), optional :: ylim !! y-axis range
1337+
character(len=*), intent (in), optional :: xscale !! example: 'linear' (default), 'log'
1338+
character(len=*), intent (in), optional :: yscale !! example: 'linear' (default), 'log'
1339+
real(wp),dimension(:), intent (in), optional :: color !! RGB color tuple [0-1,0-1,0-1]
1340+
integer, intent (out) :: istat !! status output (0 means no problems)
1341+
1342+
character(len=:), allocatable :: arg_str !! the arguments to pass to `plot`
1343+
character(len=:), allocatable :: xstr !! x values stringified
1344+
character(len=:), allocatable :: ystr !! y values stringified
1345+
character(len=:), allocatable :: xlimstr !! xlim values stringified
1346+
character(len=:), allocatable :: ylimstr !! ylim values stringified
1347+
character(len=:), allocatable :: xerrstr !! xerr values stringified
1348+
character(len=:), allocatable :: yerrstr !! yerr values stringified
1349+
character(len=:), allocatable :: color_str !! color values stringified
1350+
character(len=max_int_len) :: imark !! actual markers size
1351+
character(len=max_int_len) :: iline !! actual line width
1352+
character(len=*), parameter :: xname = 'x' !! x variable name for script
1353+
character(len=*), parameter :: yname = 'y' !! y variable name for script
1354+
character(len=*), parameter :: xerrname = 'xerr' !! xerr variable name for script
1355+
character(len=*), parameter :: yerrname = 'yerr' !! yerr variable name for script
1356+
1357+
if (allocated(me%str)) then
1358+
1359+
istat = 0
1360+
1361+
!axis limits (optional):
1362+
if (present(xlim)) call vec_to_string(xlim, me%real_fmt, xlimstr, me%use_numpy)
1363+
if (present(ylim)) call vec_to_string(ylim, me%real_fmt, ylimstr, me%use_numpy)
1364+
!errorbar sizes (optional):
1365+
if (present(xerr)) call vec_to_string(xerr, me%real_fmt, xerrstr, me%use_numpy)
1366+
if (present(yerr)) call vec_to_string(yerr, me%real_fmt, yerrstr, me%use_numpy)
1367+
1368+
!convert the arrays to strings:
1369+
call vec_to_string(x, me%real_fmt, xstr, me%use_numpy)
1370+
call vec_to_string(y, me%real_fmt, ystr, me%use_numpy)
1371+
1372+
!get optional inputs (if not present, set default value):
1373+
call optional_int_to_string(markersize, imark, '3')
1374+
call optional_int_to_string(linewidth, iline, '3')
1375+
1376+
!write the arrays:
1377+
call me%add_str(trim(xname)//' = '//xstr)
1378+
call me%add_str(trim(yname)//' = '//ystr)
1379+
call me%add_str('')
1380+
if (present(xerr)) call me%add_str(trim(xerrname)//' = '//xerrstr)
1381+
if (present(yerr)) call me%add_str(trim(yerrname)//' = '//yerrstr)
1382+
if (present(xerr) .or. present(yerr)) call me%add_str('')
1383+
1384+
!main arguments for plot:
1385+
arg_str = trim(xname)//','//&
1386+
trim(yname)//','//&
1387+
'fmt="'//trim(linestyle)//'",'//&
1388+
'linewidth='//trim(adjustl(iline))//','//&
1389+
'markersize='//trim(adjustl(imark))//','//&
1390+
'label="'//trim(label)//'"'
1391+
1392+
! optional arguments:
1393+
if (present(xerr)) then
1394+
arg_str = arg_str//','//'xerr='//trim(xerrname)
1395+
end if
1396+
if (present(yerr)) then
1397+
arg_str = arg_str//','//'yerr='//trim(yerrname)
1398+
end if
1399+
if (present(color)) then
1400+
if (size(color)<=3) then
1401+
call vec_to_string(color(1:3), '*', color_str, use_numpy=.false., is_tuple=.true.)
1402+
arg_str = arg_str//',color='//trim(color_str)
1403+
end if
1404+
end if
1405+
1406+
!write the plot statement:
1407+
call me%add_str('ax.errorbar('//arg_str//')')
1408+
1409+
!axis limits:
1410+
if (allocated(xlimstr)) call me%add_str('ax.set_xlim('//xlimstr//')')
1411+
if (allocated(ylimstr)) call me%add_str('ax.set_ylim('//ylimstr//')')
1412+
1413+
!axis scales:
1414+
if (present(xscale)) call me%add_str('ax.set_xscale("'//xscale//'")')
1415+
if (present(yscale)) call me%add_str('ax.set_yscale("'//yscale//'")')
1416+
1417+
call me%add_str('')
1418+
1419+
else
1420+
istat = -1
1421+
write(error_unit,'(A)') 'Error in add_errorbar: pyplot class not properly initialized.'
1422+
end if
1423+
1424+
end subroutine add_errorbar
1425+
!*****************************************************************************************
1426+
13181427
!*****************************************************************************************
13191428
end module pyplot_module
13201429
!*****************************************************************************************

0 commit comments

Comments
 (0)