Skip to content

Commit 2b794bf

Browse files
committed
minor changes and additional error checking.
1 parent bf37066 commit 2b794bf

File tree

1 file changed

+77
-55
lines changed

1 file changed

+77
-55
lines changed

src/pyplot_module.f90

Lines changed: 77 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -205,11 +205,15 @@ subroutine initialize(me,grid,xlabel,ylabel,title,legend,use_numpy,figsize)
205205
if (me%use_numpy) call me%add_str('import numpy as np')
206206
call me%add_str('')
207207

208-
call me%add_str('matplotlib.rcParams["font.size"] = 10.0')
208+
!.... these also need to be optional user inputs ....
209+
210+
call me%add_str('matplotlib.rcParams["font.size"] = 10')
209211
call me%add_str('matplotlib.rcParams["font.family"] = "Serif"')
210-
call me%add_str('matplotlib.rcParams["axes.labelsize"] = 10.0')
211-
call me%add_str('matplotlib.rcParams["xtick.labelsize"] = 10.0')
212-
call me%add_str('matplotlib.rcParams["ytick.labelsize"] = 10.0')
212+
call me%add_str('matplotlib.rcParams["axes.labelsize"] = 10')
213+
call me%add_str('matplotlib.rcParams["xtick.labelsize"] = 10')
214+
call me%add_str('matplotlib.rcParams["ytick.labelsize"] = 10')
215+
call me%add_str('matplotlib.rcParams["legend.fontsize"] = 10')
216+
213217
call me%add_str('')
214218

215219
if (present(figsize)) then !if specifying the figure size
@@ -261,28 +265,34 @@ subroutine add_plot(me,x,y,label,linestyle,markersize,linewidth)
261265
character(len=*),parameter :: xname = 'x' !variable names for script
262266
character(len=*),parameter :: yname = 'y' !
263267

264-
!convert the arrays to strings:
265-
call vec_to_string(x,xstr,me%use_numpy)
266-
call vec_to_string(y,ystr,me%use_numpy)
268+
if (allocated(me%str)) then
267269

268-
!get optional inputs (if not present, set default value):
269-
call optional_int_to_string(markersize,imark,'3')
270-
call optional_int_to_string(linewidth, iline,'3')
270+
!convert the arrays to strings:
271+
call vec_to_string(x,xstr,me%use_numpy)
272+
call vec_to_string(y,ystr,me%use_numpy)
271273

272-
!write the arrays:
273-
call me%add_str(trim(xname)//' = '//xstr)
274-
call me%add_str(trim(yname)//' = '//ystr)
275-
call me%add_str('')
274+
!get optional inputs (if not present, set default value):
275+
call optional_int_to_string(markersize,imark,'3')
276+
call optional_int_to_string(linewidth, iline,'3')
276277

277-
!write the plot statement:
278-
call me%add_str('ax.plot('//&
279-
trim(xname)//','//&
280-
trim(yname)//','//&
281-
'"'//trim(linestyle)//'",'//&
282-
'linewidth='//trim(adjustl(iline))//','//&
283-
'markersize='//trim(adjustl(imark))//','//&
284-
'label="'//trim(label)//'")')
285-
call me%add_str('')
278+
!write the arrays:
279+
call me%add_str(trim(xname)//' = '//xstr)
280+
call me%add_str(trim(yname)//' = '//ystr)
281+
call me%add_str('')
282+
283+
!write the plot statement:
284+
call me%add_str('ax.plot('//&
285+
trim(xname)//','//&
286+
trim(yname)//','//&
287+
'"'//trim(linestyle)//'",'//&
288+
'linewidth='//trim(adjustl(iline))//','//&
289+
'markersize='//trim(adjustl(imark))//','//&
290+
'label="'//trim(label)//'")')
291+
call me%add_str('')
292+
293+
else
294+
error stop 'Error in add_plot: pyplot class not properly initialized.'
295+
end if
286296

287297
end subroutine add_plot
288298
!*****************************************************************************************
@@ -317,31 +327,37 @@ subroutine add_bar(me,left,height,label,width,bottom,color)
317327
character(len=*),parameter :: wname = 'w' !
318328
character(len=*),parameter :: bname = 'b' !
319329

320-
!convert the arrays to strings:
321-
call vec_to_string(left,xstr, me%use_numpy)
322-
call vec_to_string(height,ystr,me%use_numpy)
323-
if (present(width)) call vec_to_string(width,wstr, me%use_numpy)
324-
if (present(bottom)) call vec_to_string(bottom,bstr,me%use_numpy)
325-
326-
!write the arrays:
327-
call me%add_str(trim(xname)//' = '//xstr)
328-
call me%add_str(trim(yname)//' = '//ystr)
329-
if (present(width)) call me%add_str(trim(wname)//' = '//wstr)
330-
if (present(bottom)) call me%add_str(trim(bname)//' = '//bstr)
331-
call me%add_str('')
330+
if (allocated(me%str)) then
332331

333-
!create the plot string:
334-
plt_str = 'ax.bar('//&
335-
'left='//trim(xname)//','//&
336-
'height='//trim(yname)//','
337-
if (present(width)) plt_str=plt_str//'width='//trim(wname)//','
338-
if (present(bottom)) plt_str=plt_str//'bottom='//trim(bstr)//','
339-
if (present(color)) plt_str=plt_str//'color="'//trim(color)//'",'
340-
plt_str=plt_str//'label="'//trim(label)//'")'
341-
342-
!write the plot statement:
343-
call me%add_str(plt_str)
344-
call me%add_str('')
332+
!convert the arrays to strings:
333+
call vec_to_string(left,xstr, me%use_numpy)
334+
call vec_to_string(height,ystr,me%use_numpy)
335+
if (present(width)) call vec_to_string(width,wstr, me%use_numpy)
336+
if (present(bottom)) call vec_to_string(bottom,bstr,me%use_numpy)
337+
338+
!write the arrays:
339+
call me%add_str(trim(xname)//' = '//xstr)
340+
call me%add_str(trim(yname)//' = '//ystr)
341+
if (present(width)) call me%add_str(trim(wname)//' = '//wstr)
342+
if (present(bottom)) call me%add_str(trim(bname)//' = '//bstr)
343+
call me%add_str('')
344+
345+
!create the plot string:
346+
plt_str = 'ax.bar('//&
347+
'left='//trim(xname)//','//&
348+
'height='//trim(yname)//','
349+
if (present(width)) plt_str=plt_str//'width='//trim(wname)//','
350+
if (present(bottom)) plt_str=plt_str//'bottom='//trim(bstr)//','
351+
if (present(color)) plt_str=plt_str//'color="'//trim(color)//'",'
352+
plt_str=plt_str//'label="'//trim(label)//'")'
353+
354+
!write the plot statement:
355+
call me%add_str(plt_str)
356+
call me%add_str('')
357+
358+
else
359+
error stop 'Error in add_bar: pyplot class not properly initialized.'
360+
end if
345361

346362
end subroutine add_bar
347363
!*****************************************************************************************
@@ -509,15 +525,21 @@ subroutine savefig(me,figfile,pyfile)
509525
character(len=*),intent(in) :: figfile !file name for the figure
510526
character(len=*),intent(in),optional :: pyfile !name of the python script to generate
511527

512-
!finish up the string:
513-
if (me%show_legend) then
514-
call me%add_str('ax.legend(loc="best")')
515-
call me%add_str('')
516-
end if
517-
call me%add_str('plt.savefig("'//trim(figfile)//'")')
528+
if (allocated(me%str)) then
529+
530+
!finish up the string:
531+
if (me%show_legend) then
532+
call me%add_str('ax.legend(loc="best")')
533+
call me%add_str('')
534+
end if
535+
call me%add_str('plt.savefig("'//trim(figfile)//'")')
518536

519-
!run it:
520-
call me%execute(pyfile)
537+
!run it:
538+
call me%execute(pyfile)
539+
540+
else
541+
error stop 'error in savefig: pyplot class not properly initialized.'
542+
end if
521543

522544
end subroutine savefig
523545
!*****************************************************************************************

0 commit comments

Comments
 (0)