Skip to content

Commit ed9aa06

Browse files
committed
2 parents fd13449 + 53d366e commit ed9aa06

File tree

2 files changed

+121
-1
lines changed

2 files changed

+121
-1
lines changed

src/pyplot_module.f90

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ module pyplot_module
5454
procedure, public :: add_contour !! add a contour plot to pyplot instance
5555
procedure, public :: add_bar !! add a barplot to pyplot instance
5656
procedure, public :: add_imshow !! add an image plot (using `imshow`)
57-
57+
procedure, public :: add_hist !! add a histogram plot to pyplot instance
5858
procedure, public :: savefig !! save plots of pyplot instance
5959
procedure, public :: destroy !! destroy pyplot instance
6060

@@ -302,6 +302,90 @@ subroutine add_plot(me, x, y, label, linestyle, markersize, linewidth, xlim, yli
302302
end subroutine add_plot
303303
!*****************************************************************************************
304304

305+
!*****************************************************************************************
306+
!> author: Jimmy Leta
307+
!
308+
! Add a histogram plot.
309+
310+
subroutine add_hist(me, x, label, xlim, ylim, xscale, yscale, bins, normed, cumulative)
311+
312+
class(pyplot), intent (inout) :: me !! pyplot handler
313+
real(wp), dimension(:), intent (in) :: x !! array of data
314+
character(len=*), intent (in) :: label !! plot label
315+
real(wp),dimension(2), intent (in), optional :: xlim !! x-axis range
316+
real(wp),dimension(2), intent (in), optional :: ylim !! y-axis range
317+
character(len=*), intent (in), optional :: xscale !! example: 'linear' (default), 'log'
318+
character(len=*), intent (in), optional :: yscale !! example: 'linear' (default), 'log'
319+
integer, intent (in), optional :: bins !! number of bins
320+
logical, intent (in), optional :: normed !! boolean flag that determines whether bin counts are normalized
321+
logical, intent (in), optional :: cumulative !! boolean flag that determines whether histogram represents the cumulative density of dataset
322+
323+
character(len=:), allocatable :: xstr !! x values stringified
324+
character(len=:), allocatable :: xlimstr !! xlim values stringified
325+
character(len=:), allocatable :: ylimstr !! ylim values stringified
326+
character(len=*), parameter :: xname = 'x' !! x variable name for script
327+
character(len=:), allocatable :: extras !! optional stuff
328+
character(len=5) :: normedstr='' !!
329+
character(len=5) :: cumulativestr='' !!
330+
character(len=max_int_len) :: binsstr !!
331+
332+
333+
if (allocated(me%str)) then
334+
335+
!axis limits (optional):
336+
if (present(xlim)) call vec_to_string(xlim, me%real_fmt, xlimstr, me%use_numpy)
337+
if (present(ylim)) call vec_to_string(ylim, me%real_fmt, ylimstr, me%use_numpy)
338+
339+
!convert the arrays to strings:
340+
call vec_to_string(x, me%real_fmt, xstr, me%use_numpy)
341+
342+
!write the arrays:
343+
call me%add_str(trim(xname)//' = '//xstr)
344+
call me%add_str('')
345+
346+
!get optional inputs (if not present, set default value):
347+
call optional_int_to_string(bins, binsstr, '10')
348+
349+
!optional arguments:
350+
if (present(bins)) extras = extras//','//'bins="'//binsstr//'"'
351+
352+
if (present(normed) .and. normed) then
353+
normedstr = 'True'
354+
else
355+
normedstr = 'False'
356+
end if
357+
358+
if (present(cumulative) .and. cumulative) then
359+
cumulativestr = 'True'
360+
else
361+
cumulativestr = 'False'
362+
end if
363+
364+
!write the plot statement:
365+
call me%add_str('ax.hist('//&
366+
trim(xname)//','//&
367+
'label="'//trim(label)//'",'//&
368+
'bins='//trim(binsstr)//','//&
369+
'cumulative='//trim(cumulativestr)//','//&
370+
'normed='//trim(normedstr)//')')
371+
372+
!axis limits:
373+
if (allocated(xlimstr)) call me%add_str('ax.set_xlim('//xlimstr//')')
374+
if (allocated(ylimstr)) call me%add_str('ax.set_ylim('//ylimstr//')')
375+
376+
!axis scales:
377+
if (present(xscale)) call me%add_str('ax.set_xscale("'//xscale//'")')
378+
if (present(yscale)) call me%add_str('ax.set_yscale("'//yscale//'")')
379+
380+
call me%add_str('')
381+
382+
else
383+
error stop 'Error in add_plot: pyplot class not properly initialized.'
384+
end if
385+
386+
end subroutine add_hist
387+
!*****************************************************************************************
388+
305389
!*****************************************************************************************
306390
!> author: Jacob Williams
307391
!

src/tests/test.f90

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,5 +85,41 @@ program test
8585
call plt%add_imshow(mat,xlim=[0.0_wp, 100.0_wp],ylim=[0.0_wp, 100.0_wp])
8686
call plt%savefig('imshow.png', pyfile='imshow.py')
8787

88+
!histogram chart:
89+
x = [0.194,0.501,-1.241,1.425,-2.217,-0.342,-0.979,0.909,0.994,0.101, &
90+
-0.131,-0.627,0.463,1.404,0.036,-2.000,0.109,1.250,-1.035,-1.115, &
91+
0.935,0.496,1.100,0.770,-1.307,-0.693,-0.072,-1.331,-0.701, &
92+
-0.494,0.666,-0.313,-0.430,-0.654,1.638,-0.334,-0.418,0.550,-0.034, &
93+
0.473,0.704,0.801,-0.157,0.055,-0.057,-1.049,-1.022,0.495,0.756, &
94+
0.149,0.543,-0.813,-0.171,-0.994,-1.532,0.502,1.324,-0.593,-0.467, &
95+
0.372,-0.904,1.255,0.931,-0.779,1.529,-0.036,0.783,0.292,-0.792, &
96+
-0.223,-0.325,0.225,-0.492,-0.941,0.065,1.300,-1.241,-1.124,-0.499, &
97+
1.233,-0.845,-0.948,-1.060,1.103,-1.154,-0.594,0.335,-1.423,0.571, &
98+
-0.903,1.129,-0.372,-1.043,-1.327,0.147,1.056,1.068,-0.699,0.988,-0.630]
99+
100+
call plt%initialize(grid=.true.,xlabel='x',&
101+
title='hist test',&
102+
legend=.true.,figsize=[20,10],&
103+
font_size = 20,&
104+
axes_labelsize = 20,&
105+
xtick_labelsize = 20,&
106+
ytick_labelsize = 20,&
107+
legend_fontsize = 20 )
108+
109+
call plt%add_hist(x=x, label='x', normed=.true.)
110+
call plt%savefig('histtest1.png', pyfile='histtest1.py')
111+
112+
call plt%initialize(grid=.true.,xlabel='x',&
113+
title='cumulative hist test',&
114+
legend=.true.,figsize=[20,10],&
115+
font_size = 20,&
116+
axes_labelsize = 20,&
117+
xtick_labelsize = 20,&
118+
ytick_labelsize = 20,&
119+
legend_fontsize = 20 )
120+
121+
call plt%add_hist(x=x, label='x', bins=8, cumulative=.true.)
122+
call plt%savefig('histtest2.png', pyfile='histtest2.py')
123+
88124
end program test
89125
!*****************************************************************************************

0 commit comments

Comments
 (0)