@@ -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!
0 commit comments