@@ -65,6 +65,11 @@ module pyplot_module
65
65
character (len=* ),parameter :: python_exe = ' python'
66
66
! *********************************************************
67
67
68
+ character (len=* ),parameter :: int_fmt = ' (I10)' ! integer format string
69
+ integer ,parameter :: max_int_len = 10 ! max string length for integers
70
+ character (len=* ),parameter :: real_fmt = ' (E30.16)' ! real number format string
71
+ integer ,parameter :: max_real_len = 30 ! max string length for reals
72
+
68
73
! *********************************************************
69
74
! ****c* pyplot_module/pyplot
70
75
!
@@ -158,7 +163,7 @@ end subroutine add_str
158
163
!
159
164
! SOURCE
160
165
161
- subroutine initialize (me , grid , xlabel , ylabel , title , legend , use_numpy )
166
+ subroutine initialize (me ,grid ,xlabel ,ylabel ,title ,legend ,use_numpy , figsize )
162
167
163
168
implicit none
164
169
@@ -169,6 +174,9 @@ subroutine initialize(me, grid, xlabel, ylabel, title, legend, use_numpy)
169
174
character (len=* ),intent (in ),optional :: title
170
175
logical ,intent (in ),optional :: legend
171
176
logical ,intent (in ),optional :: use_numpy
177
+ integer ,dimension (2 ),intent (in ),optional :: figsize
178
+
179
+ character (len= max_int_len) :: width_str, height_str
172
180
173
181
call me% destroy()
174
182
@@ -182,6 +190,10 @@ subroutine initialize(me, grid, xlabel, ylabel, title, legend, use_numpy)
182
190
else
183
191
me% use_numpy = .true.
184
192
end if
193
+ if (present (figsize)) then
194
+ call integer_to_string(figsize(1 ), width_str)
195
+ call integer_to_string(figsize(2 ), height_str)
196
+ end if
185
197
186
198
me% str = ' '
187
199
@@ -200,8 +212,12 @@ subroutine initialize(me, grid, xlabel, ylabel, title, legend, use_numpy)
200
212
call me% add_str(' matplotlib.rcParams["ytick.labelsize"] = 10.0' )
201
213
call me% add_str(' ' )
202
214
203
- call me% add_str(' fig, ax = plt.subplots()' )
204
- call me% add_str(' ' )
215
+ if (present (figsize)) then ! if specifying the figure size
216
+ call me% add_str(' fig = plt.figure(figsize=(' // trim (width_str)// ' ,' // trim (height_str)// ' ))' )
217
+ else
218
+ call me% add_str(' fig = plt.figure()' )
219
+ end if
220
+ call me% add_str(' ax = fig.gca()' )
205
221
206
222
if (present (grid)) then
207
223
if (grid) call me% add_str(' ax.grid()' )
@@ -211,6 +227,8 @@ subroutine initialize(me, grid, xlabel, ylabel, title, legend, use_numpy)
211
227
if (present (ylabel)) call me% add_str(' ax.set_ylabel("' // trim (ylabel)// ' ")' )
212
228
if (present (title)) call me% add_str(' ax.set_title("' // trim (title) // ' ")' )
213
229
230
+ call me% add_str(' ' )
231
+
214
232
end subroutine initialize
215
233
! *****************************************************************************************
216
234
@@ -238,7 +256,7 @@ subroutine add_plot(me,x,y,label,linestyle,markersize,linewidth)
238
256
integer ,intent (in ),optional :: linewidth
239
257
240
258
character (len= :),allocatable :: xstr,ystr
241
- character (len= 10 ) :: imark,iline
259
+ character (len= max_int_len ) :: imark,iline
242
260
243
261
character (len=* ),parameter :: xname = ' x' ! variable names for script
244
262
character (len=* ),parameter :: yname = ' y' !
@@ -252,9 +270,9 @@ subroutine add_plot(me,x,y,label,linestyle,markersize,linewidth)
252
270
call optional_int_to_string(linewidth, iline,' 3' )
253
271
254
272
! write the arrays:
255
- call me% add_str(' ' )
256
273
call me% add_str(trim (xname)// ' = ' // xstr)
257
274
call me% add_str(trim (yname)// ' = ' // ystr)
275
+ call me% add_str(' ' )
258
276
259
277
! write the plot statement:
260
278
call me% add_str(' ax.plot(' // &
@@ -264,6 +282,7 @@ subroutine add_plot(me,x,y,label,linestyle,markersize,linewidth)
264
282
' linewidth=' // trim (adjustl (iline))// ' ,' // &
265
283
' markersize=' // trim (adjustl (imark))// ' ,' // &
266
284
' label="' // trim (label)// ' ")' )
285
+ call me% add_str(' ' )
267
286
268
287
end subroutine add_plot
269
288
! *****************************************************************************************
@@ -279,7 +298,7 @@ end subroutine add_plot
279
298
!
280
299
! SOURCE
281
300
282
- subroutine add_bar (me ,left ,height ,label ,width ,bottom )
301
+ subroutine add_bar (me ,left ,height ,label ,width ,bottom , color )
283
302
284
303
implicit none
285
304
@@ -289,6 +308,7 @@ subroutine add_bar(me,left,height,label,width,bottom)
289
308
character (len=* ),intent (in ) :: label
290
309
real (wp),dimension (:),intent (in ),optional :: width
291
310
real (wp),dimension (:),intent (in ),optional :: bottom
311
+ character (len=* ),intent (in ),optional :: color
292
312
293
313
character (len= :),allocatable :: xstr,ystr,wstr,bstr,plt_str
294
314
@@ -304,22 +324,24 @@ subroutine add_bar(me,left,height,label,width,bottom)
304
324
if (present (bottom)) call vec_to_string(bottom,bstr,me% use_numpy)
305
325
306
326
! write the arrays:
307
- call me% add_str(' ' )
308
327
call me% add_str(trim (xname)// ' = ' // xstr)
309
328
call me% add_str(trim (yname)// ' = ' // ystr)
310
329
if (present (width)) call me% add_str(trim (wname)// ' = ' // wstr)
311
330
if (present (bottom)) call me% add_str(trim (bname)// ' = ' // bstr)
331
+ call me% add_str(' ' )
312
332
313
333
! create the plot string:
314
334
plt_str = ' ax.bar(' // &
315
335
' left=' // trim (xname)// ' ,' // &
316
336
' height=' // trim (yname)// ' ,'
317
337
if (present (width)) plt_str= plt_str// ' width=' // trim (wname)// ' ,'
318
338
if (present (bottom)) plt_str= plt_str// ' bottom=' // trim (bstr)// ' ,'
339
+ if (present (color)) plt_str= plt_str// ' color=' // trim (color)// ' ,'
319
340
plt_str= plt_str// ' label="' // trim (label)// ' ")'
320
341
321
342
! write the plot statement:
322
343
call me% add_str(plt_str)
344
+ call me% add_str(' ' )
323
345
324
346
end subroutine add_bar
325
347
! *****************************************************************************************
@@ -344,19 +366,46 @@ subroutine optional_int_to_string(int_value,string_value,default_value)
344
366
character (len=* ),intent (out ) :: string_value
345
367
character (len=* ),intent (in ) :: default_value
346
368
347
- character (len=* ),parameter :: int_fmt = ' (I10)' ! integer format string
348
- integer :: istat
349
-
350
369
if (present (int_value)) then
351
- write (string_value,int_fmt,iostat= istat) int_value
352
- if (istat/= 0 ) error stop ' Error converting integer to string'
370
+ call integer_to_string(int_value, string_value)
353
371
else
354
372
string_value = default_value
355
373
end if
356
374
357
375
end subroutine optional_int_to_string
358
376
! *****************************************************************************************
359
377
378
+ ! *****************************************************************************************
379
+ ! ****f* pyplot_module/integer_to_string
380
+ !
381
+ ! NAME
382
+ ! integer_to_string
383
+ !
384
+ ! DESCRIPTION
385
+ ! Integer to string conversion.
386
+ !
387
+ ! SOURCE
388
+
389
+ subroutine integer_to_string (i ,s )
390
+
391
+ implicit none
392
+
393
+ integer ,intent (in ),optional :: i
394
+ character (len=* ),intent (out ) :: s
395
+
396
+ integer :: istat
397
+
398
+ write (s,int_fmt,iostat= istat) i
399
+
400
+ if (istat/= 0 ) then
401
+ error stop ' Error converting integer to string'
402
+ else
403
+ s = adjustl (s)
404
+ end if
405
+
406
+ end subroutine integer_to_string
407
+ ! *****************************************************************************************
408
+
360
409
! *****************************************************************************************
361
410
! ****f* pyplot_module/vec_to_string
362
411
!
@@ -377,12 +426,11 @@ subroutine vec_to_string(v,str,use_numpy)
377
426
logical ,intent (in ) :: use_numpy
378
427
379
428
integer :: i,istat
380
- character (len=* ),parameter :: fmt = ' (E30.16)' ! real number format string
381
- character (len= 30 ) :: tmp
429
+ character (len= max_real_len) :: tmp
382
430
383
431
str = ' ['
384
432
do i= 1 ,size (v)
385
- write (tmp,fmt ,iostat= istat) v(i)
433
+ write (tmp,real_fmt ,iostat= istat) v(i)
386
434
if (istat/= 0 ) error stop ' Error in vec_to_string'
387
435
str = str// trim (adjustl (tmp))
388
436
if (i< size (v)) str = str // ' ,'
@@ -463,10 +511,9 @@ subroutine savefig(me,figfile,pyfile)
463
511
464
512
! finish up the string:
465
513
if (me% show_legend) then
466
- call me% add_str(' ' )
467
514
call me% add_str(' ax.legend(loc="best")' )
515
+ call me% add_str(' ' )
468
516
end if
469
- call me% add_str(' ' )
470
517
call me% add_str(' plt.savefig("' // trim (figfile)// ' ")' )
471
518
472
519
! run it:
0 commit comments