Skip to content

Commit 6c675ea

Browse files
committed
added new options to savefig()
add showfig()
1 parent 24e9673 commit 6c675ea

File tree

2 files changed

+91
-16
lines changed

2 files changed

+91
-16
lines changed

src/pyplot_module.f90

+87-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
!*****************************************************************************************
22
!> author: Jacob Williams
3-
! date: 4/14/2015
3+
! date: 6/16/2017
44
! license: BSD
55
!
66
! For making simple x-y plots from Fortran.
@@ -56,11 +56,13 @@ module pyplot_module
5656
procedure, public :: add_imshow !! add an image plot (using `imshow`)
5757
procedure, public :: add_hist !! add a histogram plot to pyplot instance
5858
procedure, public :: savefig !! save plots of pyplot instance
59+
procedure, public :: showfig !! show plots of pyplot instance
5960
procedure, public :: destroy !! destroy pyplot instance
6061

6162
! private methods
62-
procedure :: execute !! execute pyplot commands
63-
procedure :: add_str !! add string to pytplot instance buffer
63+
procedure :: execute !! execute pyplot commands
64+
procedure :: add_str !! add string to pytplot instance buffer
65+
procedure :: finish_ops !! some final ops before saving
6466

6567
end type pyplot
6668

@@ -844,29 +846,70 @@ subroutine execute(me, pyfile)
844846
end subroutine execute
845847
!*****************************************************************************************
846848

849+
!*****************************************************************************************
850+
!> author: Jacob Williams
851+
!
852+
! Some final things to add before saving or showing the figure.
853+
854+
subroutine finish_ops(me)
855+
856+
class(pyplot),intent(inout) :: me !! pyplot handler
857+
858+
if (me%show_legend) then
859+
call me%add_str('ax.legend(loc="best")')
860+
call me%add_str('')
861+
end if
862+
if (me%axis_equal) then
863+
call me%add_str('ax.axis("equal")')
864+
call me%add_str('')
865+
end if
866+
867+
end subroutine finish_ops
868+
!*****************************************************************************************
869+
847870
!*****************************************************************************************
848871
!> author: Jacob Williams
849872
!
850873
! Save the figure.
874+
!
875+
!### History
876+
! * modified: Johannes Rieke 6/16/2017
877+
! * modified: Jacob Williams 6/16/2017
851878

852-
subroutine savefig(me, figfile, pyfile)
879+
subroutine savefig(me, figfile, pyfile, dpi, transparent, facecolor, edgecolor, orientation)
853880

854-
class(pyplot), intent(inout) :: me !! pyplot handler
855-
character(len=*), intent(in) :: figfile !! file name for the figure
856-
character(len=*), intent(in), optional :: pyfile !! name of the Python script to generate
881+
class(pyplot), intent(inout) :: me !! pyplot handler
882+
character(len=*), intent(in) :: figfile !! file name for the figure
883+
character(len=*), intent(in), optional :: pyfile !! name of the Python script to generate
884+
character(len=*), intent(in), optional :: dpi !! resolution of the figure for png
885+
!! [note this is a string]
886+
logical, intent(in), optional :: transparent !! transparent background (T/F)
887+
character(len=*), intent(in), optional :: facecolor !! the colors of the figure rectangle
888+
character(len=*), intent(in), optional :: edgecolor !! the colors of the figure rectangle
889+
character(len=*), intent(in), optional :: orientation !! 'landscape' or 'portrait'
890+
891+
character(len=:),allocatable :: tmp !! for building the `savefig` arguments.
857892

858893
if (allocated(me%str)) then
859894

860895
!finish up the string:
861-
if (me%show_legend) then
862-
call me%add_str('ax.legend(loc="best")')
863-
call me%add_str('')
896+
call me%finish_ops()
897+
898+
!build the savefig arguments:
899+
tmp = '"'//trim(figfile)//'"'
900+
if (present(dpi)) tmp = tmp//', dpi='//trim(dpi)
901+
if (present(transparent)) then
902+
if (transparent) then
903+
tmp = tmp//', transparent=True'
904+
else
905+
tmp = tmp//', transparent=False'
906+
end if
864907
end if
865-
if (me%axis_equal) then
866-
call me%add_str('ax.axis("equal")')
867-
call me%add_str('')
868-
end if
869-
call me%add_str('plt.savefig("'//trim(figfile)//'")')
908+
if (present(facecolor)) tmp = tmp//', facecolor="'//trim(facecolor)//'"'
909+
if (present(edgecolor)) tmp = tmp//', edgecolor="'//trim(edgecolor)//'"'
910+
if (present(orientation)) tmp = tmp//', orientation="'//trim(orientation)//'"'
911+
call me%add_str('plt.savefig('//tmp//')')
912+
deallocate(tmp)
870913

871914
!run it:
872915
call me%execute(pyfile)
@@ -878,6 +921,35 @@ subroutine savefig(me, figfile, pyfile)
878921
end subroutine savefig
879922
!*****************************************************************************************
880923

924+
!*****************************************************************************************
925+
!> author: Johannes Rieke
926+
! date: 6/16/2017
927+
!
928+
! Shows the figure.
929+
930+
subroutine showfig(me, pyfile)
931+
932+
class(pyplot), intent(inout) :: me !! pyplot handler
933+
character(len=*), intent(in), optional :: pyfile !! name of the Python script to generate
934+
935+
if (allocated(me%str)) then
936+
937+
!finish up the string:
938+
call me%finish_ops()
939+
940+
!show figure:
941+
call me%add_str('plt.show()')
942+
943+
!run it:
944+
call me%execute(pyfile)
945+
946+
else
947+
error stop 'error in showfig: pyplot class not properly initialized.'
948+
end if
949+
950+
end subroutine showfig
951+
!*****************************************************************************************
952+
881953
!*****************************************************************************************
882954
end module pyplot_module
883955
!*****************************************************************************************

src/tests/test.f90

+4-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,10 @@ program test
119119
legend_fontsize = 20 )
120120

121121
call plt%add_hist(x=x, label='x', bins=8, cumulative=.true.)
122-
call plt%savefig('histtest2.png', pyfile='histtest2.py')
122+
call plt%savefig('histtest2.png', &
123+
pyfile='histtest2.py', &
124+
dpi='200', &
125+
transparent=.true.)
123126

124127
end program test
125128
!*****************************************************************************************

0 commit comments

Comments
 (0)