@@ -38,6 +38,7 @@ module pyplot_module
38
38
39
39
logical :: show_legend = .false. ! ! show legend into plot
40
40
logical :: use_numpy = .true. ! ! use numpy python module
41
+ logical :: use_oo_api = .false. ! ! use OO interface of matplotlib (incopatible with showfig subroutine)
41
42
logical :: mplot3d = .false. ! ! it is a 3d plot
42
43
logical :: polar = .false. ! ! it is a polar plot
43
44
logical :: axis_equal = .false. ! ! equal scale on each axis
@@ -107,7 +108,7 @@ end subroutine add_str
107
108
108
109
subroutine initialize (me , grid , xlabel , ylabel , zlabel , title , legend , use_numpy , figsize , &
109
110
font_size , axes_labelsize , xtick_labelsize , ytick_labelsize , ztick_labelsize , &
110
- legend_fontsize , mplot3d , axis_equal , polar , real_fmt )
111
+ legend_fontsize , mplot3d , axis_equal , polar , real_fmt , use_oo_api )
111
112
112
113
class(pyplot), intent (inout ) :: me ! ! pyplot handler
113
114
logical , intent (in ), optional :: grid ! ! activate grid drawing
@@ -128,6 +129,7 @@ subroutine initialize(me, grid, xlabel, ylabel, zlabel, title, legend, use_numpy
128
129
logical , intent (in ), optional :: axis_equal ! ! set true for axis = 'equal'
129
130
logical , intent (in ), optional :: polar ! ! set true for polar plots (cannot use with mplot3d)
130
131
character (len=* ), intent (in ), optional :: real_fmt ! ! format string for real numbers (examples: '(E30.16)' [default], '*')
132
+ logical , intent (in ), optional :: use_oo_api ! ! avoid matplotlib's GUI by using the OO interface (cannot use with showfig)
131
133
132
134
character (len= max_int_len) :: width_str ! ! figure width dummy string
133
135
character (len= max_int_len) :: height_str ! ! figure height dummy string
@@ -139,6 +141,8 @@ subroutine initialize(me, grid, xlabel, ylabel, zlabel, title, legend, use_numpy
139
141
character (len= max_int_len) :: legend_fontsize_str ! ! size of legend font dummy string
140
142
141
143
character (len=* ), parameter :: default_font_size_str = ' 10' ! ! the default font size for plots
144
+
145
+ character (:), allocatable :: python_fig_func ! ! Python's function for creating a new Figure instance
142
146
143
147
call me% destroy()
144
148
@@ -152,6 +156,11 @@ subroutine initialize(me, grid, xlabel, ylabel, zlabel, title, legend, use_numpy
152
156
else
153
157
me% use_numpy = .true.
154
158
end if
159
+ if (present (use_oo_api)) then
160
+ me% use_oo_api = use_oo_api
161
+ else
162
+ me% use_oo_api = .false.
163
+ end if
155
164
if (present (figsize)) then
156
165
call integer_to_string(figsize(1 ), width_str)
157
166
call integer_to_string(figsize(2 ), height_str)
@@ -190,7 +199,12 @@ subroutine initialize(me, grid, xlabel, ylabel, zlabel, title, legend, use_numpy
190
199
call me% add_str(' ' )
191
200
192
201
call me% add_str(' import matplotlib' )
193
- call me% add_str(' import matplotlib.pyplot as plt' )
202
+ if (me% use_oo_api) then
203
+ call me% add_str(' from matplotlib.figure import Figure' )
204
+ call me% add_str(' from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas' )
205
+ else
206
+ call me% add_str(' import matplotlib.pyplot as plt' )
207
+ endif
194
208
if (me% mplot3d) call me% add_str(' from mpl_toolkits.mplot3d import Axes3D' )
195
209
if (me% use_numpy) call me% add_str(' import numpy as np' )
196
210
call me% add_str(' ' )
@@ -203,11 +217,16 @@ subroutine initialize(me, grid, xlabel, ylabel, zlabel, title, legend, use_numpy
203
217
call me% add_str(' matplotlib.rcParams["legend.fontsize"] = ' // trim (legend_fontsize_str))
204
218
205
219
call me% add_str(' ' )
206
-
220
+
221
+ if (me% use_oo_api) then
222
+ python_fig_func = ' Figure'
223
+ else
224
+ python_fig_func = ' plt.figure'
225
+ endif
207
226
if (present (figsize)) then ! if specifying the figure size
208
- call me% add_str(' fig = plt.figure (figsize=(' // trim (width_str)// ' ,' // trim (height_str)// ' ),facecolor="white")' )
227
+ call me% add_str(' fig = ' // python_fig_func // ' (figsize=(' // trim (width_str)// ' ,' // trim (height_str)// ' ),facecolor="white")' )
209
228
else
210
- call me% add_str(' fig = plt.figure (facecolor="white")' )
229
+ call me% add_str(' fig = ' // python_fig_func // ' (facecolor="white")' )
211
230
end if
212
231
213
232
if (me% mplot3d) then
@@ -1117,7 +1136,12 @@ subroutine savefig(me, figfile, pyfile, dpi, transparent, facecolor, edgecolor,
1117
1136
if (present (facecolor)) tmp = tmp// ' , facecolor="' // trim (facecolor)// ' "'
1118
1137
if (present (edgecolor)) tmp = tmp// ' , edgecolor="' // trim (edgecolor)// ' "'
1119
1138
if (present (orientation)) tmp = tmp// ' , orientation="' // trim (orientation)// ' "'
1120
- call me% add_str(' plt.savefig(' // tmp// ' )' )
1139
+ if (me% use_oo_api) then
1140
+ call me% add_str(' canvas = FigureCanvas(fig)' )
1141
+ call me% add_str(' canvas.print_figure(' // tmp// ' )' )
1142
+ else
1143
+ call me% add_str(' plt.savefig(' // tmp// ' )' )
1144
+ endif
1121
1145
deallocate (tmp)
1122
1146
1123
1147
! run it:
@@ -1143,8 +1167,15 @@ subroutine showfig(me, pyfile, istat)
1143
1167
character (len=* ), intent (in ), optional :: pyfile ! ! name of the Python script to generate
1144
1168
integer , intent (out ) :: istat ! ! status output (0 means no problems)
1145
1169
1146
- if (allocated (me% str)) then
1147
-
1170
+ if (.not. allocated (me% str)) then
1171
+ istat = - 1
1172
+ write (error_unit,' (A)' ) ' error in showfig: pyplot class not properly initialized.'
1173
+
1174
+ elseif (me% use_oo_api) then
1175
+ istat = - 2
1176
+ write (error_unit,' (A)' ) " error in showfig: not compatible with 'use_oo_api' option"
1177
+
1178
+ else
1148
1179
istat = 0
1149
1180
1150
1181
! finish up the string:
@@ -1156,9 +1187,6 @@ subroutine showfig(me, pyfile, istat)
1156
1187
! run it:
1157
1188
call me% execute(pyfile, istat= istat)
1158
1189
1159
- else
1160
- istat = - 1
1161
- write (error_unit,' (A)' ) ' error in showfig: pyplot class not properly initialized.'
1162
1190
end if
1163
1191
1164
1192
end subroutine showfig
0 commit comments