Skip to content

Commit 5b13776

Browse files
committed
added error bar support for bar charts
1 parent f96f7bc commit 5b13776

File tree

2 files changed

+29
-22
lines changed

2 files changed

+29
-22
lines changed

src/pyplot_module.f90

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ end subroutine add_3d_plot
405405
!
406406
! Add a bar plot.
407407

408-
subroutine add_bar(me, left, height, label, width, bottom, color)
408+
subroutine add_bar(me, left, height, label, width, bottom, color, yerr)
409409

410410
class(pyplot), intent(inout) :: me !! pyplot handler
411411
real(wp), dimension(:), intent(in) :: left !! left bar values
@@ -414,36 +414,42 @@ subroutine add_bar(me, left, height, label, width, bottom, color)
414414
real(wp), dimension(:), intent(in), optional :: width !! width values
415415
real(wp), dimension(:), intent(in), optional :: bottom !! bottom values
416416
character(len=*), intent(in), optional :: color !! plot color
417-
418-
character(len=:), allocatable :: xstr !! x axis values stringified
419-
character(len=:), allocatable :: ystr !! y axis values stringified
420-
character(len=:), allocatable :: wstr !! width values stringified
421-
character(len=:), allocatable :: bstr !! bottom values stringified
422-
character(len=:), allocatable :: plt_str !! plot string
423-
character(len=*), parameter :: xname = 'x' !! x axis name
424-
character(len=*), parameter :: yname = 'y' !! y axis name
425-
character(len=*), parameter :: wname = 'w' !! width name
426-
character(len=*), parameter :: bname = 'b' !! bottom name
417+
real(wp), dimension(:), intent(in), optional :: yerr !! yerr values
418+
419+
character(len=:), allocatable :: xstr !! x axis values stringified
420+
character(len=:), allocatable :: ystr !! y axis values stringified
421+
character(len=:), allocatable :: wstr !! width values stringified
422+
character(len=:), allocatable :: bstr !! bottom values stringified
423+
character(len=:), allocatable :: plt_str !! plot string
424+
character(len=:), allocatable :: yerr_str !! yerr values stringified
425+
character(len=*), parameter :: xname = 'x' !! x axis name
426+
character(len=*), parameter :: yname = 'y' !! y axis name
427+
character(len=*), parameter :: wname = 'w' !! width name
428+
character(len=*), parameter :: bname = 'b' !! bottom name
429+
character(len=*), parameter :: yerrname = 'yerr' !! yerr name
427430

428431
if (allocated(me%str)) then
429432

430433
!convert the arrays to strings:
431-
call vec_to_string(left, xstr, me%use_numpy)
432-
call vec_to_string(height, ystr, me%use_numpy)
433-
if (present(width)) call vec_to_string(width, wstr, me%use_numpy)
434-
if (present(bottom)) call vec_to_string(bottom, bstr, me%use_numpy)
434+
call vec_to_string(left, xstr, me%use_numpy)
435+
call vec_to_string(height, ystr, me%use_numpy)
436+
if (present(width)) call vec_to_string(width, wstr, me%use_numpy)
437+
if (present(bottom)) call vec_to_string(bottom, bstr, me%use_numpy)
438+
if (present(yerr)) call vec_to_string(yerr, yerr_str, me%use_numpy)
435439

436440
!write the arrays:
437441
call me%add_str(trim(xname)//' = '//xstr)
438442
call me%add_str(trim(yname)//' = '//ystr)
439443
if (present(width)) call me%add_str(trim(wname)//' = '//wstr)
440444
if (present(bottom)) call me%add_str(trim(bname)//' = '//bstr)
445+
if (present(yerr)) call me%add_str(trim(yerrname)//' = '//yerr_str)
441446
call me%add_str('')
442447

443448
!create the plot string:
444449
plt_str = 'ax.bar('//&
445450
'left='//trim(xname)//','//&
446451
'height='//trim(yname)//','
452+
if (present(yerr)) plt_str=plt_str//'yerr='//trim(yerrname)//','
447453
if (present(width)) plt_str=plt_str//'width='//trim(wname)//','
448454
if (present(bottom)) plt_str=plt_str//'bottom='//trim(bstr)//','
449455
if (present(color)) plt_str=plt_str//'color="'//trim(color)//'",'
@@ -544,7 +550,6 @@ subroutine matrix_to_string(v, str, use_numpy)
544550
logical, intent(in) :: use_numpy !! activate numpy python module usage
545551

546552
integer :: i !! counter
547-
integer :: j !! counter
548553
character(len=:),allocatable :: tmp !! dummy string
549554

550555
str = '['

src/tests/test.f90

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,20 @@ program test
1212

1313
implicit none
1414

15-
real(wp), dimension(100) :: x !! x values
15+
real(wp), dimension(100) :: x !! x values
16+
real(wp), dimension(100) :: yerr !! error values for bar chart
1617
real(wp), dimension(100) :: sx !! sin(x) values
1718
real(wp), dimension(100) :: cx !! cos(x) values
1819
real(wp), dimension(100) :: tx !! sin(x)*cos(x) values
1920
type(pyplot) :: plt !! pytplot handler
2021
integer :: i !! counter
2122

2223
!generate some data:
23-
x = [(real(i,wp), i=0,size(x)-1)]/5.0_wp
24-
sx = sin(x)
25-
cx = cos(x)
26-
tx = sx * cx
24+
x = [(real(i,wp), i=0,size(x)-1)]/5.0_wp
25+
sx = sin(x)
26+
cx = cos(x)
27+
tx = sx * cx
28+
yerr = abs(sx*.25_wp)
2729

2830
!2d line plot:
2931
call plt%initialize(grid=.true.,xlabel='angle (rad)',&
@@ -42,7 +44,7 @@ program test
4244
xtick_labelsize = 20,&
4345
ytick_labelsize = 20,&
4446
legend_fontsize = 20 )
45-
call plt%add_bar(left=x,height=sx,width=tx,label='$\sin (x)$',color='r')
47+
call plt%add_bar(left=x,height=sx,width=tx,label='$\sin (x)$',color='r',yerr=yerr)
4648
call plt%savefig('bartest.png', pyfile='bartest.py')
4749

4850
end program test

0 commit comments

Comments
 (0)