1
+ ! *****************************************************************************************
1
2
! > author: Jacob Williams
2
3
! date: 4/14/2015
3
4
! license: BSD
4
5
!
5
- ! # DESCRIPTION
6
6
! For making simple x-y plots from Fortran.
7
7
! It works by generating a Python script and executing it.
8
8
!
9
- ! # SEE ALSO
9
+ ! # See also
10
10
! * Inspired by: [EasyPlot](https://pypi.python.org/pypi/EasyPlot)
11
- module pyplot_module
12
11
13
- use , intrinsic :: iso_fortran_env, only : real64
12
+ module pyplot_module
14
13
15
- implicit none
14
+ use , intrinsic :: iso_fortran_env, only : real64
16
15
17
- private
16
+ implicit none
18
17
19
- integer , parameter , private :: wp = real64 ! ! Default real kind [8 bytes].
20
-
21
- character (len=* ), parameter :: tmp_file = ' pyplot_module_temp_1234567890.py' ! ! Default name of the temporary file
22
- ! ! (this can also be user-specified).
23
-
24
- character (len=* ), parameter :: python_exe = ' python' ! ! The python executable name.
25
- character (len=* ), parameter :: int_fmt = ' (I10)' ! ! integer format string
26
- integer , parameter :: max_int_len = 10 ! ! max string length for integers
27
- character (len=* ), parameter :: real_fmt = ' (E30.16)' ! ! real number format string
28
- integer , parameter :: max_real_len = 30 ! ! max string length for reals
18
+ private
29
19
20
+ integer , parameter , private :: wp = real64 ! ! Default real kind [8 bytes].
21
+
22
+ character (len=* ), parameter :: tmp_file = ' pyplot_module_temp_1234567890.py' ! ! Default name of the temporary file
23
+ ! ! (this can also be user-specified).
24
+
25
+ character (len=* ), parameter :: python_exe = ' python' ! ! The python executable name.
26
+ character (len=* ), parameter :: int_fmt = ' (I10)' ! ! integer format string
27
+ integer , parameter :: max_int_len = 10 ! ! max string length for integers
28
+ character (len=* ), parameter :: real_fmt = ' (E30.16)' ! ! real number format string
29
+ integer , parameter :: max_real_len = 30 ! ! max string length for reals
30
+
31
+ type, public :: pyplot
32
+
33
+ ! ! The main pyplot class.
34
+
35
+ private
36
+
37
+ character (len= :), allocatable :: str ! ! string buffer
38
+
39
+ logical :: show_legend = .false. ! ! show legend into plot
40
+ logical :: use_numpy = .true. ! ! use numpy python module
41
+
42
+ contains
43
+
44
+ ! public methods
45
+ procedure , public :: initialize ! ! initialize pyplot instance
46
+ procedure , public :: add_plot ! ! add a plot to pyplot instance
47
+ procedure , public :: add_bar ! ! add a barplot to pyplot instance
48
+ procedure , public :: savefig ! ! save plots of pyplot instance
49
+ procedure , public :: destroy ! ! destroy pyplot instance
50
+
51
+ ! private methods
52
+ procedure :: execute ! ! execute pyplot commands
53
+ procedure :: add_str ! ! add string to pytplot instance buffer
54
+
55
+ end type pyplot
56
+
57
+ contains
58
+ ! *****************************************************************************************
59
+
60
+ ! *****************************************************************************************
30
61
! > author: Jacob Williams
31
62
!
32
- ! The main pyplot class.
33
- type, public :: pyplot
34
- private
63
+ ! Destructor.
35
64
36
- character (len= :), allocatable :: str ! ! string buffer
37
-
38
- logical :: show_legend = .false. ! ! show legend into plot
39
- logical :: use_numpy = .true. ! ! use numpy python module
40
- contains
41
- ! public methods
42
- procedure , public :: initialize ! ! initialize pyplot instance
43
- procedure , public :: add_plot ! ! add a plot to pyplot instance
44
- procedure , public :: add_bar ! ! add a barplot to pyplot instance
45
- procedure , public :: savefig ! ! save plots of pyplot instance
46
- procedure , public :: destroy ! ! destroy pyplot instance
47
- ! private methods
48
- procedure :: execute ! ! execute pyplot commands
49
- procedure :: add_str ! ! add string to pytplot instance buffer
50
- end type pyplot
51
- contains
52
-
53
- ! > author: Jacob Williams
54
- !
55
- ! Destructor
56
65
subroutine destroy (me )
66
+
57
67
class(pyplot),intent (inout ) :: me ! ! pyplot handler
58
68
59
69
if (allocated (me% str)) deallocate (me% str)
70
+
60
71
end subroutine destroy
72
+ ! *****************************************************************************************
73
+
74
+ ! *****************************************************************************************
75
+ ! > author: Jacob Williams
76
+ !
77
+ ! Add a string to the buffer.
61
78
62
- ! > author: Jacob Williams
63
- !
64
- ! Add a string to the buffer.
65
79
subroutine add_str (me ,str )
80
+
66
81
class(pyplot), intent (inout ) :: me ! ! pyplot handler
67
82
character (len=* ), intent (in ) :: str ! ! str to be added to pyplot handler buffer
68
83
69
84
me% str = me% str// str// new_line(' ' )
85
+
70
86
end subroutine add_str
87
+ ! *****************************************************************************************
88
+
89
+ ! *****************************************************************************************
90
+ ! > author: Jacob Williams
91
+ !
92
+ ! Initialize a plot
71
93
72
- ! > author: Jacob Williams
73
- !
74
- ! Initialize a plot
75
94
subroutine initialize (me , grid , xlabel , ylabel , title , legend , use_numpy , figsize , &
76
95
font_size , axes_labelsize , xtick_labelsize , ytick_labelsize , legend_fontsize )
96
+
77
97
class(pyplot), intent (inout ) :: me ! ! pyplot handler
78
98
logical , intent (in ), optional :: grid ! ! activate grid drawing
79
99
character (len=* ), intent (in ), optional :: xlabel ! ! label of x axis
@@ -153,12 +173,17 @@ subroutine initialize(me, grid, xlabel, ylabel, title, legend, use_numpy, figsiz
153
173
if (present (title)) call me% add_str(' ax.set_title("' // trim (title) // ' ")' )
154
174
155
175
call me% add_str(' ' )
176
+
156
177
end subroutine initialize
178
+ ! *****************************************************************************************
179
+
180
+ ! *****************************************************************************************
181
+ ! > author: Jacob Williams
182
+ !
183
+ ! Add an x,y plot.
157
184
158
- ! > author: Jacob Williams
159
- !
160
- ! Add an x,y plot.
161
185
subroutine add_plot (me , x , y , label , linestyle , markersize , linewidth )
186
+
162
187
class(pyplot), intent (inout ) :: me ! ! pyplot handler
163
188
real (wp), dimension (:), intent (in ) :: x ! ! x values
164
189
real (wp), dimension (:), intent (in ) :: y ! ! y values
@@ -201,12 +226,17 @@ subroutine add_plot(me, x, y, label, linestyle, markersize, linewidth)
201
226
else
202
227
error stop ' Error in add_plot: pyplot class not properly initialized.'
203
228
end if
229
+
204
230
end subroutine add_plot
231
+ ! *****************************************************************************************
205
232
206
- ! > author: Jacob Williams
207
- !
208
- ! Add a bar plot.
233
+ ! *****************************************************************************************
234
+ ! > author: Jacob Williams
235
+ !
236
+ ! Add a bar plot.
237
+
209
238
subroutine add_bar (me , left , height , label , width , bottom , color )
239
+
210
240
class(pyplot), intent (inout ) :: me ! ! pyplot handler
211
241
real (wp), dimension (:), intent (in ) :: left ! ! left bar values
212
242
real (wp), dimension (:), intent (in ) :: height ! ! height bar values
@@ -257,12 +287,16 @@ subroutine add_bar(me, left, height, label, width, bottom, color)
257
287
end if
258
288
259
289
end subroutine add_bar
290
+ ! *****************************************************************************************
291
+
292
+ ! *****************************************************************************************
293
+ ! > author: Jacob Williams
294
+ !
295
+ ! Integer to string, specifying the default value if
296
+ ! the optional argument is not present.
260
297
261
- ! > author: Jacob Williams
262
- !
263
- ! Integer to string, specifying the default value if
264
- ! the optional argument is not present.
265
298
subroutine optional_int_to_string (int_value , string_value , default_value )
299
+
266
300
integer , intent (in ), optional :: int_value ! ! integer value
267
301
character (len=* ), intent (out ) :: string_value ! ! integer value stringified
268
302
character (len=* ), intent (in ) :: default_value ! ! default integer value
@@ -274,10 +308,13 @@ subroutine optional_int_to_string(int_value, string_value, default_value)
274
308
end if
275
309
276
310
end subroutine optional_int_to_string
311
+ ! *****************************************************************************************
312
+
313
+ ! *****************************************************************************************
314
+ ! > author: Jacob Williams
315
+ !
316
+ ! Integer to string conversion.
277
317
278
- ! > author: Jacob Williams
279
- !
280
- ! Integer to string conversion.
281
318
subroutine integer_to_string (i , s )
282
319
integer , intent (in ), optional :: i ! ! integer value
283
320
character (len=* ), intent (out ) :: s ! ! integer value stringified
@@ -292,11 +329,15 @@ subroutine integer_to_string(i, s)
292
329
end if
293
330
294
331
end subroutine integer_to_string
332
+ ! *****************************************************************************************
333
+
334
+ ! *****************************************************************************************
335
+ ! > author: Jacob Williams
336
+ !
337
+ ! Real vector to string.
295
338
296
- ! > author: Jacob Williams
297
- !
298
- ! Real vector to string.
299
339
subroutine vec_to_string (v , str , use_numpy )
340
+
300
341
real (wp), dimension (:), intent (in ) :: v ! ! real values
301
342
character (len= :), allocatable , intent (out ) :: str ! ! real values stringified
302
343
logical , intent (in ) :: use_numpy ! ! activate numpy python module usage
@@ -317,11 +358,15 @@ subroutine vec_to_string(v, str, use_numpy)
317
358
if (use_numpy) str = ' np.array(' // str// ' )'
318
359
319
360
end subroutine vec_to_string
361
+ ! *****************************************************************************************
362
+
363
+ ! *****************************************************************************************
364
+ ! > author: Jacob Williams
365
+ !
366
+ ! Write the buffer to a file, and then execute it with Python.
320
367
321
- ! > author: Jacob Williams
322
- !
323
- ! Write the buffer to a file, and then execute it with Python.
324
368
subroutine execute (me , pyfile )
369
+
325
370
class(pyplot), intent (inout ) :: me ! ! pytplot handler
326
371
character (len=* ), intent (in ), optional :: pyfile ! ! name of the python script to generate
327
372
integer :: istat ! ! IO status
@@ -350,12 +395,17 @@ subroutine execute(me, pyfile)
350
395
deallocate (file)
351
396
352
397
end if
398
+
353
399
end subroutine execute
400
+ ! *****************************************************************************************
401
+
402
+ ! *****************************************************************************************
403
+ ! > author: Jacob Williams
404
+ !
405
+ ! Save the figure.
354
406
355
- ! > author: Jacob Williams
356
- !
357
- ! Save the figure.
358
407
subroutine savefig (me , figfile , pyfile )
408
+
359
409
class(pyplot), intent (inout ) :: me ! ! pyplot handler
360
410
character (len=* ), intent (in ) :: figfile ! ! file name for the figure
361
411
character (len=* ), intent (in ), optional :: pyfile ! ! name of the Python script to generate
@@ -377,4 +427,8 @@ subroutine savefig(me, figfile, pyfile)
377
427
end if
378
428
379
429
end subroutine savefig
380
- end module pyplot_module
430
+ ! *****************************************************************************************
431
+
432
+ ! *****************************************************************************************
433
+ end module pyplot_module
434
+ ! *****************************************************************************************
0 commit comments