@@ -38,15 +38,17 @@ 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 :: mplot3d = .false. ! ! it is a 3d plot
41
42
42
43
contains
43
44
44
45
! 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
46
+ procedure , public :: initialize ! ! initialize pyplot instance
47
+ procedure , public :: add_plot ! ! add a 2d plot to pyplot instance
48
+ procedure , public :: add_3d_plot ! ! add a 3d plot to pyplot instance
49
+ procedure , public :: add_bar ! ! add a barplot to pyplot instance
50
+ procedure , public :: savefig ! ! save plots of pyplot instance
51
+ procedure , public :: destroy ! ! destroy pyplot instance
50
52
51
53
! private methods
52
54
procedure :: execute ! ! execute pyplot commands
@@ -91,30 +93,35 @@ end subroutine add_str
91
93
!
92
94
! Initialize a plot
93
95
94
- subroutine initialize (me , grid , xlabel , ylabel , title , legend , use_numpy , figsize , &
95
- font_size , axes_labelsize , xtick_labelsize , ytick_labelsize , legend_fontsize )
96
+ subroutine initialize (me , grid , xlabel , ylabel , zlabel , title , legend , use_numpy , figsize , &
97
+ font_size , axes_labelsize , xtick_labelsize , ytick_labelsize , ztick_labelsize , &
98
+ legend_fontsize , mplot3d )
96
99
97
- class(pyplot), intent (inout ) :: me ! ! pyplot handler
98
- logical , intent (in ), optional :: grid ! ! activate grid drawing
99
- character (len=* ), intent (in ), optional :: xlabel ! ! label of x axis
100
- character (len=* ), intent (in ), optional :: ylabel ! ! label of y axis
101
- character (len=* ), intent (in ), optional :: title ! ! plot title
102
- logical , intent (in ), optional :: legend ! ! plot legend
103
- logical , intent (in ), optional :: use_numpy ! ! activate usage of numpy python module
104
- integer , dimension (2 ), intent (in ), optional :: figsize ! ! dimension of the figure
105
- integer , intent (in ), optional :: font_size ! ! font size
106
- integer , intent (in ), optional :: axes_labelsize ! ! size of axis labels
107
- integer , intent (in ), optional :: xtick_labelsize ! ! size of x axis tick lables
108
- integer , intent (in ), optional :: ytick_labelsize ! ! size of y axis tick lables
109
- integer , intent (in ), optional :: legend_fontsize ! ! size of legend font
100
+ class(pyplot), intent (inout ) :: me ! ! pyplot handler
101
+ logical , intent (in ), optional :: grid ! ! activate grid drawing
102
+ character (len=* ), intent (in ), optional :: xlabel ! ! label of x axis
103
+ character (len=* ), intent (in ), optional :: ylabel ! ! label of y axis
104
+ character (len=* ), intent (in ), optional :: zlabel ! ! label of z axis
105
+ character (len=* ), intent (in ), optional :: title ! ! plot title
106
+ logical , intent (in ), optional :: legend ! ! plot legend
107
+ logical , intent (in ), optional :: use_numpy ! ! activate usage of numpy python module
108
+ integer , dimension (2 ), intent (in ), optional :: figsize ! ! dimension of the figure
109
+ integer , intent (in ), optional :: font_size ! ! font size
110
+ integer , intent (in ), optional :: axes_labelsize ! ! size of axis labels
111
+ integer , intent (in ), optional :: xtick_labelsize ! ! size of x axis tick lables
112
+ integer , intent (in ), optional :: ytick_labelsize ! ! size of y axis tick lables
113
+ integer , intent (in ), optional :: ztick_labelsize ! ! size of z axis tick lables
114
+ integer , intent (in ), optional :: legend_fontsize ! ! size of legend font
115
+ logical , intent (in ), optional :: mplot3d ! ! set true for 3d plots
110
116
111
117
character (len= max_int_len) :: width_str ! ! figure width dummy string
112
118
character (len= max_int_len) :: height_str ! ! figure height dummy string
113
119
character (len= max_int_len) :: font_size_str ! ! font size dummy string
114
120
character (len= max_int_len) :: axes_labelsize_str ! ! size of axis labels dummy string
115
- character (len= max_int_len) :: xtick_labelsize_str ! ! sise of x axis tick labels dummy string
116
- character (len= max_int_len) :: ytick_labelsize_str ! ! sise of x axis tick labels dummy string
117
- character (len= max_int_len) :: legend_fontsize_str ! ! sise of legend font dummy string
121
+ character (len= max_int_len) :: xtick_labelsize_str ! ! size of x axis tick labels dummy string
122
+ character (len= max_int_len) :: ytick_labelsize_str ! ! size of x axis tick labels dummy string
123
+ character (len= max_int_len) :: ztick_labelsize_str ! ! size of z axis tick labels dummy string
124
+ character (len= max_int_len) :: legend_fontsize_str ! ! size of legend font dummy string
118
125
character (len=* ), parameter :: default_font_size_str = ' 10' ! ! the default font size for plots
119
126
120
127
call me% destroy()
@@ -133,10 +140,17 @@ subroutine initialize(me, grid, xlabel, ylabel, title, legend, use_numpy, figsiz
133
140
call integer_to_string(figsize(1 ), width_str)
134
141
call integer_to_string(figsize(2 ), height_str)
135
142
end if
143
+ if (present (mplot3d)) then
144
+ me% mplot3d = mplot3d
145
+ else
146
+ me% mplot3d = .false.
147
+ end if
148
+
136
149
call optional_int_to_string(font_size, font_size_str, default_font_size_str)
137
150
call optional_int_to_string(axes_labelsize, axes_labelsize_str, default_font_size_str)
138
151
call optional_int_to_string(xtick_labelsize, xtick_labelsize_str, default_font_size_str)
139
152
call optional_int_to_string(ytick_labelsize, ytick_labelsize_str, default_font_size_str)
153
+ call optional_int_to_string(ztick_labelsize, ztick_labelsize_str, default_font_size_str)
140
154
call optional_int_to_string(legend_fontsize, legend_fontsize_str, default_font_size_str)
141
155
142
156
me% str = ' '
@@ -146,6 +160,7 @@ subroutine initialize(me, grid, xlabel, ylabel, title, legend, use_numpy, figsiz
146
160
147
161
call me% add_str(' import matplotlib' )
148
162
call me% add_str(' import matplotlib.pyplot as plt' )
163
+ if (me% mplot3d) call me% add_str(' from mpl_toolkits.mplot3d import Axes3D' )
149
164
if (me% use_numpy) call me% add_str(' import numpy as np' )
150
165
call me% add_str(' ' )
151
166
@@ -163,16 +178,22 @@ subroutine initialize(me, grid, xlabel, ylabel, title, legend, use_numpy, figsiz
163
178
else
164
179
call me% add_str(' fig = plt.figure()' )
165
180
end if
166
- call me% add_str(' ax = fig.gca()' )
167
-
181
+
182
+ if (me% mplot3d) then
183
+ call me% add_str(' ax = fig.gca(projection='' 3d'' )' )
184
+ else
185
+ call me% add_str(' ax = fig.gca()' )
186
+ end if
187
+
168
188
if (present (grid)) then
169
189
if (grid) call me% add_str(' ax.grid()' )
170
190
end if
171
191
172
192
if (present (xlabel)) call me% add_str(' ax.set_xlabel("' // trim (xlabel)// ' ")' )
173
193
if (present (ylabel)) call me% add_str(' ax.set_ylabel("' // trim (ylabel)// ' ")' )
194
+ if (present (zlabel)) call me% add_str(' ax.set_zlabel("' // trim (zlabel)// ' ")' )
174
195
if (present (title)) call me% add_str(' ax.set_title("' // trim (title) // ' ")' )
175
-
196
+
176
197
call me% add_str(' ' )
177
198
178
199
end subroutine initialize
@@ -232,6 +253,68 @@ subroutine add_plot(me, x, y, label, linestyle, markersize, linewidth)
232
253
end subroutine add_plot
233
254
! *****************************************************************************************
234
255
256
+ ! *****************************************************************************************
257
+ ! > author: Jacob Williams
258
+ !
259
+ ! Add a 3D x,y,z plot.
260
+ !
261
+ ! @note Must initialize the class with ```mplot3d=.true.```
262
+
263
+ subroutine add_3d_plot (me , x , y , z , label , linestyle , markersize , linewidth )
264
+
265
+ class(pyplot), intent (inout ) :: me ! ! pyplot handler
266
+ real (wp), dimension (:), intent (in ) :: x ! ! x values
267
+ real (wp), dimension (:), intent (in ) :: y ! ! y values
268
+ real (wp), dimension (:), intent (in ) :: z ! ! z values
269
+ character (len=* ), intent (in ) :: label ! ! plot label
270
+ character (len=* ), intent (in ) :: linestyle ! ! style of the plot line
271
+ integer , intent (in ), optional :: markersize ! ! size of the plot markers
272
+ integer , intent (in ), optional :: linewidth ! ! width of the plot line
273
+
274
+ character (len= :), allocatable :: xstr ! ! x values strinfied
275
+ character (len= :), allocatable :: ystr ! ! y values strinfied
276
+ character (len= :), allocatable :: zstr ! ! z values strinfied
277
+ character (len= max_int_len) :: imark ! ! actual markers size
278
+ character (len= max_int_len) :: iline ! ! actual line width
279
+ character (len=* ), parameter :: xname = ' x' ! ! x variable name for script
280
+ character (len=* ), parameter :: yname = ' y' ! ! y variable name for script
281
+ character (len=* ), parameter :: zname = ' z' ! ! z variable name for script
282
+
283
+ if (allocated (me% str)) then
284
+
285
+ ! convert the arrays to strings:
286
+ call vec_to_string(x, xstr, me% use_numpy)
287
+ call vec_to_string(y, ystr, me% use_numpy)
288
+ call vec_to_string(z, zstr, me% use_numpy)
289
+
290
+ ! get optional inputs (if not present, set default value):
291
+ call optional_int_to_string(markersize, imark, ' 3' )
292
+ call optional_int_to_string(linewidth, iline, ' 3' )
293
+
294
+ ! write the arrays:
295
+ call me% add_str(trim (xname)// ' = ' // xstr)
296
+ call me% add_str(trim (yname)// ' = ' // ystr)
297
+ call me% add_str(trim (zname)// ' = ' // zstr)
298
+ call me% add_str(' ' )
299
+
300
+ ! write the plot statement:
301
+ call me% add_str(' ax.plot(' // &
302
+ trim (xname)// ' ,' // &
303
+ trim (yname)// ' ,' // &
304
+ trim (zname)// ' ,' // &
305
+ ' "' // trim (linestyle)// ' ",' // &
306
+ ' linewidth=' // trim (adjustl (iline))// ' ,' // &
307
+ ' markersize=' // trim (adjustl (imark))// ' ,' // &
308
+ ' label="' // trim (label)// ' ")' )
309
+ call me% add_str(' ' )
310
+
311
+ else
312
+ error stop ' Error in add_3d_plot: pyplot class not properly initialized.'
313
+ end if
314
+
315
+ end subroutine add_3d_plot
316
+ ! *****************************************************************************************
317
+
235
318
! *****************************************************************************************
236
319
! > author: Jacob Williams
237
320
!
0 commit comments