@@ -38,6 +38,7 @@ module pyplot_module
3838
3939 logical :: show_legend = .false. ! ! show legend into plot
4040 logical :: use_numpy = .true. ! ! use numpy python module
41+ logical :: use_oo_api = .false. ! ! use OO interface of matplotlib (incopatible with showfig subroutine)
4142 logical :: mplot3d = .false. ! ! it is a 3d plot
4243 logical :: polar = .false. ! ! it is a polar plot
4344 logical :: axis_equal = .false. ! ! equal scale on each axis
@@ -107,7 +108,7 @@ end subroutine add_str
107108
108109 subroutine initialize (me , grid , xlabel , ylabel , zlabel , title , legend , use_numpy , figsize , &
109110 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 )
111112
112113 class(pyplot), intent (inout ) :: me ! ! pyplot handler
113114 logical , intent (in ), optional :: grid ! ! activate grid drawing
@@ -128,6 +129,7 @@ subroutine initialize(me, grid, xlabel, ylabel, zlabel, title, legend, use_numpy
128129 logical , intent (in ), optional :: axis_equal ! ! set true for axis = 'equal'
129130 logical , intent (in ), optional :: polar ! ! set true for polar plots (cannot use with mplot3d)
130131 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)
131133
132134 character (len= max_int_len) :: width_str ! ! figure width dummy string
133135 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
139141 character (len= max_int_len) :: legend_fontsize_str ! ! size of legend font dummy string
140142
141143 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
142146
143147 call me% destroy()
144148
@@ -152,6 +156,11 @@ subroutine initialize(me, grid, xlabel, ylabel, zlabel, title, legend, use_numpy
152156 else
153157 me% use_numpy = .true.
154158 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
155164 if (present (figsize)) then
156165 call integer_to_string(figsize(1 ), width_str)
157166 call integer_to_string(figsize(2 ), height_str)
@@ -190,7 +199,12 @@ subroutine initialize(me, grid, xlabel, ylabel, zlabel, title, legend, use_numpy
190199 call me% add_str(' ' )
191200
192201 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
194208 if (me% mplot3d) call me% add_str(' from mpl_toolkits.mplot3d import Axes3D' )
195209 if (me% use_numpy) call me% add_str(' import numpy as np' )
196210 call me% add_str(' ' )
@@ -203,11 +217,16 @@ subroutine initialize(me, grid, xlabel, ylabel, zlabel, title, legend, use_numpy
203217 call me% add_str(' matplotlib.rcParams["legend.fontsize"] = ' // trim (legend_fontsize_str))
204218
205219 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
207226 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")' )
209228 else
210- call me% add_str(' fig = plt.figure (facecolor="white")' )
229+ call me% add_str(' fig = ' // python_fig_func // ' (facecolor="white")' )
211230 end if
212231
213232 if (me% mplot3d) then
@@ -1117,7 +1136,12 @@ subroutine savefig(me, figfile, pyfile, dpi, transparent, facecolor, edgecolor,
11171136 if (present (facecolor)) tmp = tmp// ' , facecolor="' // trim (facecolor)// ' "'
11181137 if (present (edgecolor)) tmp = tmp// ' , edgecolor="' // trim (edgecolor)// ' "'
11191138 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
11211145 deallocate (tmp)
11221146
11231147 ! run it:
@@ -1143,8 +1167,15 @@ subroutine showfig(me, pyfile, istat)
11431167 character (len=* ), intent (in ), optional :: pyfile ! ! name of the Python script to generate
11441168 integer , intent (out ) :: istat ! ! status output (0 means no problems)
11451169
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
11481179 istat = 0
11491180
11501181 ! finish up the string:
@@ -1156,9 +1187,6 @@ subroutine showfig(me, pyfile, istat)
11561187 ! run it:
11571188 call me% execute(pyfile, istat= istat)
11581189
1159- else
1160- istat = - 1
1161- write (error_unit,' (A)' ) ' error in showfig: pyplot class not properly initialized.'
11621190 end if
11631191
11641192 end subroutine showfig
0 commit comments