Skip to content

Commit 48396cb

Browse files
committed
began implementing add_hist method
1 parent 88c1ce0 commit 48396cb

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

src/pyplot_module.f90

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ module pyplot_module
5353
procedure, public :: add_3d_plot !! add a 3d plot to pyplot instance
5454
procedure, public :: add_contour !! add a contour plot to pyplot instance
5555
procedure, public :: add_bar !! add a barplot to pyplot instance
56-
56+
procedure, public :: add_hist !! add a histogram plot to pyplot instance
5757
procedure, public :: savefig !! save plots of pyplot instance
5858
procedure, public :: destroy !! destroy pyplot instance
5959

@@ -301,6 +301,61 @@ subroutine add_plot(me, x, y, label, linestyle, markersize, linewidth, xlim, yli
301301
end subroutine add_plot
302302
!*****************************************************************************************
303303

304+
!*****************************************************************************************
305+
!> author: Jacob Williams
306+
!
307+
! Add a histogram plot.
308+
309+
subroutine add_hist(me, x, label, xlim, ylim, xscale, yscale)
310+
311+
class(pyplot), intent (inout) :: me !! pyplot handler
312+
real(wp), dimension(:), intent (in) :: x !! x values
313+
character(len=*), intent (in) :: label !! plot label
314+
real(wp),dimension(2), intent (in), optional :: xlim !! x-axis range
315+
real(wp),dimension(2), intent (in), optional :: ylim !! y-axis range
316+
character(len=*), intent (in), optional :: xscale !! example: 'linear' (default), 'log'
317+
character(len=*), intent (in), optional :: yscale !! example: 'linear' (default), 'log'
318+
319+
character(len=:), allocatable :: xstr !! x values stringified
320+
character(len=:), allocatable :: xlimstr !! xlim values stringified
321+
character(len=:), allocatable :: ylimstr !! ylim values stringified
322+
character(len=*), parameter :: xname = 'x' !! x variable name for script
323+
324+
if (allocated(me%str)) then
325+
326+
!axis limits (optional):
327+
if (present(xlim)) call vec_to_string(xlim, me%real_fmt, xlimstr, me%use_numpy)
328+
if (present(ylim)) call vec_to_string(ylim, me%real_fmt, ylimstr, me%use_numpy)
329+
330+
!convert the arrays to strings:
331+
call vec_to_string(x, me%real_fmt, xstr, me%use_numpy)
332+
333+
!write the arrays:
334+
call me%add_str(trim(xname)//' = '//xstr)
335+
call me%add_str('')
336+
337+
!write the plot statement:
338+
call me%add_str('ax.hist('//&
339+
trim(xname)//','//&
340+
'label="'//trim(label)//'")')
341+
342+
!axis limits:
343+
if (allocated(xlimstr)) call me%add_str('ax.set_xlim('//xlimstr//')')
344+
if (allocated(ylimstr)) call me%add_str('ax.set_ylim('//ylimstr//')')
345+
346+
!axis scales:
347+
if (present(xscale)) call me%add_str('ax.set_xscale("'//xscale//'")')
348+
if (present(yscale)) call me%add_str('ax.set_yscale("'//yscale//'")')
349+
350+
call me%add_str('')
351+
352+
else
353+
error stop 'Error in add_plot: pyplot class not properly initialized.'
354+
end if
355+
356+
end subroutine add_hist
357+
!*****************************************************************************************
358+
304359
!*****************************************************************************************
305360
!> author: Jacob Williams
306361
!

src/tests/test.f90

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,21 @@ program test
6868
call plt%add_contour(x, y, z, label='contour', linestyle='-', linewidth=2, filled=.true., cmap='bone')
6969
call plt%savefig('contour.png',pyfile='contour.py')
7070

71+
!histogram chart:
72+
do i=1,n
73+
x(i) = 1.0
74+
end do
75+
76+
write(*,*) x
77+
call plt%initialize(grid=.true.,xlabel='x',&
78+
title='hist test',legend=.true.,figsize=[20,10],&
79+
font_size = 20,&
80+
axes_labelsize = 20,&
81+
xtick_labelsize = 20,&
82+
ytick_labelsize = 20,&
83+
legend_fontsize = 20 )
84+
call plt%add_hist(x=x, label="x")
85+
call plt%savefig('histtest.png', pyfile='histtest.py')
86+
7187
end program test
7288
!*****************************************************************************************

0 commit comments

Comments
 (0)