@@ -53,7 +53,7 @@ module pyplot_module
53
53
procedure , public :: add_3d_plot ! ! add a 3d plot to pyplot instance
54
54
procedure , public :: add_contour ! ! add a contour plot to pyplot instance
55
55
procedure , public :: add_bar ! ! add a barplot to pyplot instance
56
-
56
+ procedure , public :: add_hist ! ! add a histogram plot to pyplot instance
57
57
procedure , public :: savefig ! ! save plots of pyplot instance
58
58
procedure , public :: destroy ! ! destroy pyplot instance
59
59
@@ -301,6 +301,61 @@ subroutine add_plot(me, x, y, label, linestyle, markersize, linewidth, xlim, yli
301
301
end subroutine add_plot
302
302
! *****************************************************************************************
303
303
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
+
304
359
! *****************************************************************************************
305
360
! > author: Jacob Williams
306
361
!
0 commit comments