1
1
! *****************************************************************************************
2
2
! > author: Jacob Williams
3
- ! date: 4/14/2015
3
+ ! date: 6/16/2017
4
4
! license: BSD
5
5
!
6
6
! For making simple x-y plots from Fortran.
@@ -56,11 +56,13 @@ module pyplot_module
56
56
procedure , public :: add_imshow ! ! add an image plot (using `imshow`)
57
57
procedure , public :: add_hist ! ! add a histogram plot to pyplot instance
58
58
procedure , public :: savefig ! ! save plots of pyplot instance
59
+ procedure , public :: showfig ! ! show plots of pyplot instance
59
60
procedure , public :: destroy ! ! destroy pyplot instance
60
61
61
62
! 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
64
66
65
67
end type pyplot
66
68
@@ -844,29 +846,70 @@ subroutine execute(me, pyfile)
844
846
end subroutine execute
845
847
! *****************************************************************************************
846
848
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
+
847
870
! *****************************************************************************************
848
871
! > author: Jacob Williams
849
872
!
850
873
! Save the figure.
874
+ !
875
+ ! ### History
876
+ ! * modified: Johannes Rieke 6/16/2017
877
+ ! * modified: Jacob Williams 6/16/2017
851
878
852
- subroutine savefig (me , figfile , pyfile )
879
+ subroutine savefig (me , figfile , pyfile , dpi , transparent , facecolor , edgecolor , orientation )
853
880
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.
857
892
858
893
if (allocated (me% str)) then
859
894
860
895
! 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
864
907
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 )
870
913
871
914
! run it:
872
915
call me% execute(pyfile)
@@ -878,6 +921,35 @@ subroutine savefig(me, figfile, pyfile)
878
921
end subroutine savefig
879
922
! *****************************************************************************************
880
923
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
+
881
953
! *****************************************************************************************
882
954
end module pyplot_module
883
955
! *****************************************************************************************
0 commit comments