Skip to content

Commit 568ef20

Browse files
committed
added xlim, ylim, and align options.
1 parent 770f233 commit 568ef20

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

src/pyplot_module.f90

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ end subroutine initialize
223223
!
224224
! Add an x,y plot.
225225

226-
subroutine add_plot(me, x, y, label, linestyle, markersize, linewidth)
226+
subroutine add_plot(me, x, y, label, linestyle, markersize, linewidth, xlim, ylim)
227227

228228
class(pyplot), intent (inout) :: me !! pyplot handler
229229
real(wp), dimension(:), intent (in) :: x !! x values
@@ -232,16 +232,24 @@ subroutine add_plot(me, x, y, label, linestyle, markersize, linewidth)
232232
character(len=*), intent (in) :: linestyle !! style of the plot line
233233
integer, intent (in), optional :: markersize !! size of the plot markers
234234
integer, intent (in), optional :: linewidth !! width of the plot line
235+
real(wp),dimension(2), intent (in), optional :: xlim !! x-axis range
236+
real(wp),dimension(2), intent (in), optional :: ylim !! y-axis range
235237

236-
character(len=:), allocatable :: xstr !! x values strinfied
237-
character(len=:), allocatable :: ystr !! y values strinfied
238+
character(len=:), allocatable :: xstr !! x values stringified
239+
character(len=:), allocatable :: ystr !! y values stringified
240+
character(len=:), allocatable :: xlimstr !! xlim values stringified
241+
character(len=:), allocatable :: ylimstr !! ylim values stringified
238242
character(len=max_int_len) :: imark !! actual markers size
239243
character(len=max_int_len) :: iline !! actual line width
240244
character(len=*), parameter :: xname = 'x' !! x variable name for script
241245
character(len=*), parameter :: yname = 'y' !! y variable name for script
242246

243247
if (allocated(me%str)) then
244248

249+
!axis limits (optional):
250+
if (present(xlim)) call vec_to_string(xlim, xlimstr, me%use_numpy)
251+
if (present(ylim)) call vec_to_string(ylim, ylimstr, me%use_numpy)
252+
245253
!convert the arrays to strings:
246254
call vec_to_string(x, xstr, me%use_numpy)
247255
call vec_to_string(y, ystr, me%use_numpy)
@@ -263,6 +271,11 @@ subroutine add_plot(me, x, y, label, linestyle, markersize, linewidth)
263271
'linewidth='//trim(adjustl(iline))//','//&
264272
'markersize='//trim(adjustl(imark))//','//&
265273
'label="'//trim(label)//'")')
274+
275+
!axis limits:
276+
if (allocated(xlimstr)) call me%add_str('ax.set_xlim('//xlimstr//')')
277+
if (allocated(ylimstr)) call me%add_str('ax.set_ylim('//ylimstr//')')
278+
266279
call me%add_str('')
267280

268281
else
@@ -414,7 +427,7 @@ end subroutine add_3d_plot
414427
!
415428
! Add a bar plot.
416429

417-
subroutine add_bar(me, left, height, label, width, bottom, color, yerr)
430+
subroutine add_bar(me, left, height, label, width, bottom, color, yerr, align, xlim, ylim)
418431

419432
class(pyplot), intent(inout) :: me !! pyplot handler
420433
real(wp), dimension(:), intent(in) :: left !! left bar values
@@ -424,9 +437,14 @@ subroutine add_bar(me, left, height, label, width, bottom, color, yerr)
424437
real(wp), dimension(:), intent(in), optional :: bottom !! bottom values
425438
character(len=*), intent(in), optional :: color !! plot color
426439
real(wp), dimension(:), intent(in), optional :: yerr !! yerr values
440+
character(len=*), intent(in), optional :: align !! default: 'center'
441+
real(wp),dimension(2), intent (in), optional :: xlim !! x-axis range
442+
real(wp),dimension(2), intent (in), optional :: ylim !! y-axis range
427443

428444
character(len=:), allocatable :: xstr !! x axis values stringified
429445
character(len=:), allocatable :: ystr !! y axis values stringified
446+
character(len=:), allocatable :: xlimstr !! xlim values stringified
447+
character(len=:), allocatable :: ylimstr !! ylim values stringified
430448
character(len=:), allocatable :: wstr !! width values stringified
431449
character(len=:), allocatable :: bstr !! bottom values stringified
432450
character(len=:), allocatable :: plt_str !! plot string
@@ -439,6 +457,10 @@ subroutine add_bar(me, left, height, label, width, bottom, color, yerr)
439457

440458
if (allocated(me%str)) then
441459

460+
!axis limits (optional):
461+
if (present(xlim)) call vec_to_string(xlim, xlimstr, me%use_numpy)
462+
if (present(ylim)) call vec_to_string(ylim, ylimstr, me%use_numpy)
463+
442464
!convert the arrays to strings:
443465
call vec_to_string(left, xstr, me%use_numpy)
444466
call vec_to_string(height, ystr, me%use_numpy)
@@ -462,10 +484,16 @@ subroutine add_bar(me, left, height, label, width, bottom, color, yerr)
462484
if (present(width)) plt_str=plt_str//'width='//trim(wname)//','
463485
if (present(bottom)) plt_str=plt_str//'bottom='//trim(bstr)//','
464486
if (present(color)) plt_str=plt_str//'color="'//trim(color)//'",'
487+
if (present(align)) plt_str=plt_str//'align="'//trim(align)//'",'
465488
plt_str=plt_str//'label="'//trim(label)//'")'
466489

467490
!write the plot statement:
468491
call me%add_str(plt_str)
492+
493+
!axis limits:
494+
if (allocated(xlimstr)) call me%add_str('ax.set_xlim('//xlimstr//')')
495+
if (allocated(ylimstr)) call me%add_str('ax.set_ylim('//ylimstr//')')
496+
469497
call me%add_str('')
470498

471499
else

src/tests/test.f90

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ program test
4444
xtick_labelsize = 20,&
4545
ytick_labelsize = 20,&
4646
legend_fontsize = 20 )
47-
call plt%add_bar(left=x,height=sx,width=tx,label='$\sin (x)$',color='r',yerr=yerr)
47+
call plt%add_bar(left=x,height=sx,width=tx,label='$\sin (x)$',&
48+
color='r',yerr=yerr,xlim=[0.0_wp, 20.0_wp],align='center')
4849
call plt%savefig('bartest.png', pyfile='bartest.py')
4950

5051
end program test

0 commit comments

Comments
 (0)