Skip to content

Commit 450c4da

Browse files
committed
added support for figure size and bar color.
1 parent 0d48046 commit 450c4da

File tree

2 files changed

+66
-19
lines changed

2 files changed

+66
-19
lines changed

src/pyplot_module.f90

Lines changed: 64 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ module pyplot_module
6565
character(len=*),parameter :: python_exe ='python'
6666
!*********************************************************
6767

68+
character(len=*),parameter :: int_fmt = '(I10)' !integer format string
69+
integer,parameter :: max_int_len = 10 !max string length for integers
70+
character(len=*),parameter :: real_fmt = '(E30.16)' !real number format string
71+
integer,parameter :: max_real_len = 30 !max string length for reals
72+
6873
!*********************************************************
6974
!****c* pyplot_module/pyplot
7075
!
@@ -158,7 +163,7 @@ end subroutine add_str
158163
!
159164
! SOURCE
160165

161-
subroutine initialize(me, grid, xlabel, ylabel, title, legend, use_numpy)
166+
subroutine initialize(me,grid,xlabel,ylabel,title,legend,use_numpy,figsize)
162167

163168
implicit none
164169

@@ -169,6 +174,9 @@ subroutine initialize(me, grid, xlabel, ylabel, title, legend, use_numpy)
169174
character(len=*),intent(in),optional :: title
170175
logical,intent(in),optional :: legend
171176
logical,intent(in),optional :: use_numpy
177+
integer,dimension(2),intent(in),optional :: figsize
178+
179+
character(len=max_int_len) :: width_str, height_str
172180

173181
call me%destroy()
174182

@@ -182,6 +190,10 @@ subroutine initialize(me, grid, xlabel, ylabel, title, legend, use_numpy)
182190
else
183191
me%use_numpy = .true.
184192
end if
193+
if (present(figsize)) then
194+
call integer_to_string(figsize(1), width_str)
195+
call integer_to_string(figsize(2), height_str)
196+
end if
185197

186198
me%str = ''
187199

@@ -200,8 +212,12 @@ subroutine initialize(me, grid, xlabel, ylabel, title, legend, use_numpy)
200212
call me%add_str('matplotlib.rcParams["ytick.labelsize"] = 10.0')
201213
call me%add_str('')
202214

203-
call me%add_str('fig, ax = plt.subplots()')
204-
call me%add_str('')
215+
if (present(figsize)) then !if specifying the figure size
216+
call me%add_str('fig = plt.figure(figsize=('//trim(width_str)//','//trim(height_str)//'))')
217+
else
218+
call me%add_str('fig = plt.figure()')
219+
end if
220+
call me%add_str('ax = fig.gca()')
205221

206222
if (present(grid)) then
207223
if (grid) call me%add_str('ax.grid()')
@@ -211,6 +227,8 @@ subroutine initialize(me, grid, xlabel, ylabel, title, legend, use_numpy)
211227
if (present(ylabel)) call me%add_str('ax.set_ylabel("'//trim(ylabel)//'")')
212228
if (present(title)) call me%add_str('ax.set_title("' //trim(title) //'")')
213229

230+
call me%add_str('')
231+
214232
end subroutine initialize
215233
!*****************************************************************************************
216234

@@ -238,7 +256,7 @@ subroutine add_plot(me,x,y,label,linestyle,markersize,linewidth)
238256
integer,intent(in),optional :: linewidth
239257

240258
character(len=:),allocatable :: xstr,ystr
241-
character(len=10) :: imark,iline
259+
character(len=max_int_len) :: imark,iline
242260

243261
character(len=*),parameter :: xname = 'x' !variable names for script
244262
character(len=*),parameter :: yname = 'y' !
@@ -252,9 +270,9 @@ subroutine add_plot(me,x,y,label,linestyle,markersize,linewidth)
252270
call optional_int_to_string(linewidth, iline,'3')
253271

254272
!write the arrays:
255-
call me%add_str('')
256273
call me%add_str(trim(xname)//' = '//xstr)
257274
call me%add_str(trim(yname)//' = '//ystr)
275+
call me%add_str('')
258276

259277
!write the plot statement:
260278
call me%add_str('ax.plot('//&
@@ -264,6 +282,7 @@ subroutine add_plot(me,x,y,label,linestyle,markersize,linewidth)
264282
'linewidth='//trim(adjustl(iline))//','//&
265283
'markersize='//trim(adjustl(imark))//','//&
266284
'label="'//trim(label)//'")')
285+
call me%add_str('')
267286

268287
end subroutine add_plot
269288
!*****************************************************************************************
@@ -279,7 +298,7 @@ end subroutine add_plot
279298
!
280299
! SOURCE
281300

282-
subroutine add_bar(me,left,height,label,width,bottom)
301+
subroutine add_bar(me,left,height,label,width,bottom,color)
283302

284303
implicit none
285304

@@ -289,6 +308,7 @@ subroutine add_bar(me,left,height,label,width,bottom)
289308
character(len=*),intent(in) :: label
290309
real(wp),dimension(:),intent(in),optional :: width
291310
real(wp),dimension(:),intent(in),optional :: bottom
311+
character(len=*),intent(in),optional :: color
292312

293313
character(len=:),allocatable :: xstr,ystr,wstr,bstr,plt_str
294314

@@ -304,22 +324,24 @@ subroutine add_bar(me,left,height,label,width,bottom)
304324
if (present(bottom)) call vec_to_string(bottom,bstr,me%use_numpy)
305325

306326
!write the arrays:
307-
call me%add_str('')
308327
call me%add_str(trim(xname)//' = '//xstr)
309328
call me%add_str(trim(yname)//' = '//ystr)
310329
if (present(width)) call me%add_str(trim(wname)//' = '//wstr)
311330
if (present(bottom)) call me%add_str(trim(bname)//' = '//bstr)
331+
call me%add_str('')
312332

313333
!create the plot string:
314334
plt_str = 'ax.bar('//&
315335
'left='//trim(xname)//','//&
316336
'height='//trim(yname)//','
317337
if (present(width)) plt_str=plt_str//'width='//trim(wname)//','
318338
if (present(bottom)) plt_str=plt_str//'bottom='//trim(bstr)//','
339+
if (present(color)) plt_str=plt_str//'color='//trim(color)//','
319340
plt_str=plt_str//'label="'//trim(label)//'")'
320341

321342
!write the plot statement:
322343
call me%add_str(plt_str)
344+
call me%add_str('')
323345

324346
end subroutine add_bar
325347
!*****************************************************************************************
@@ -344,19 +366,46 @@ subroutine optional_int_to_string(int_value,string_value,default_value)
344366
character(len=*),intent(out) :: string_value
345367
character(len=*),intent(in) :: default_value
346368

347-
character(len=*),parameter :: int_fmt = '(I10)' !integer format string
348-
integer :: istat
349-
350369
if (present(int_value)) then
351-
write(string_value,int_fmt,iostat=istat) int_value
352-
if (istat/=0) error stop 'Error converting integer to string'
370+
call integer_to_string(int_value, string_value)
353371
else
354372
string_value = default_value
355373
end if
356374

357375
end subroutine optional_int_to_string
358376
!*****************************************************************************************
359377

378+
!*****************************************************************************************
379+
!****f* pyplot_module/integer_to_string
380+
!
381+
! NAME
382+
! integer_to_string
383+
!
384+
! DESCRIPTION
385+
! Integer to string conversion.
386+
!
387+
! SOURCE
388+
389+
subroutine integer_to_string(i,s)
390+
391+
implicit none
392+
393+
integer,intent(in),optional :: i
394+
character(len=*),intent(out) :: s
395+
396+
integer :: istat
397+
398+
write(s,int_fmt,iostat=istat) i
399+
400+
if (istat/=0) then
401+
error stop 'Error converting integer to string'
402+
else
403+
s = adjustl(s)
404+
end if
405+
406+
end subroutine integer_to_string
407+
!*****************************************************************************************
408+
360409
!*****************************************************************************************
361410
!****f* pyplot_module/vec_to_string
362411
!
@@ -377,12 +426,11 @@ subroutine vec_to_string(v,str,use_numpy)
377426
logical,intent(in) :: use_numpy
378427

379428
integer :: i,istat
380-
character(len=*),parameter :: fmt = '(E30.16)' !real number format string
381-
character(len=30) :: tmp
429+
character(len=max_real_len) :: tmp
382430

383431
str = '['
384432
do i=1,size(v)
385-
write(tmp,fmt,iostat=istat) v(i)
433+
write(tmp,real_fmt,iostat=istat) v(i)
386434
if (istat/=0) error stop 'Error in vec_to_string'
387435
str = str//trim(adjustl(tmp))
388436
if (i<size(v)) str = str // ','
@@ -463,10 +511,9 @@ subroutine savefig(me,figfile,pyfile)
463511

464512
!finish up the string:
465513
if (me%show_legend) then
466-
call me%add_str('')
467514
call me%add_str('ax.legend(loc="best")')
515+
call me%add_str('')
468516
end if
469-
call me%add_str('')
470517
call me%add_str('plt.savefig("'//trim(figfile)//'")')
471518

472519
!run it:

src/test.f90

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ program test
4040
!bar chart:
4141
tx = 0.1_wp !for bar width
4242
call plt%initialize(grid=.true.,xlabel='angle (rad)',&
43-
title='bar test',legend=.true.)
44-
call plt%add_bar(left=x,height=sx,width=tx,label='$\sin (x)$')
43+
title='bar test',legend=.true.,figsize=[20,5])
44+
call plt%add_bar(left=x,height=sx,width=tx,label='$\sin (x)$',color='r')
4545
call plt%savefig('bartest.png', pyfile='bartest.py')
4646

4747
end program test

0 commit comments

Comments
 (0)