Skip to content

Commit 3e91de1

Browse files
committed
added initial support for 3d plots.
1 parent 6a02696 commit 3e91de1

File tree

1 file changed

+109
-26
lines changed

1 file changed

+109
-26
lines changed

src/pyplot_module.f90

Lines changed: 109 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,17 @@ module pyplot_module
3838

3939
logical :: show_legend = .false. !! show legend into plot
4040
logical :: use_numpy = .true. !! use numpy python module
41+
logical :: mplot3d = .false. !! it is a 3d plot
4142

4243
contains
4344

4445
! 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
5052

5153
! private methods
5254
procedure :: execute !! execute pyplot commands
@@ -91,30 +93,35 @@ end subroutine add_str
9193
!
9294
! Initialize a plot
9395

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)
9699

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
110116

111117
character(len=max_int_len) :: width_str !! figure width dummy string
112118
character(len=max_int_len) :: height_str !! figure height dummy string
113119
character(len=max_int_len) :: font_size_str !! font size dummy string
114120
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
118125
character(len=*), parameter :: default_font_size_str = '10' !! the default font size for plots
119126

120127
call me%destroy()
@@ -133,10 +140,17 @@ subroutine initialize(me, grid, xlabel, ylabel, title, legend, use_numpy, figsiz
133140
call integer_to_string(figsize(1), width_str)
134141
call integer_to_string(figsize(2), height_str)
135142
end if
143+
if (present(mplot3d)) then
144+
me%mplot3d = mplot3d
145+
else
146+
me%mplot3d = .false.
147+
end if
148+
136149
call optional_int_to_string(font_size, font_size_str, default_font_size_str)
137150
call optional_int_to_string(axes_labelsize, axes_labelsize_str, default_font_size_str)
138151
call optional_int_to_string(xtick_labelsize, xtick_labelsize_str, default_font_size_str)
139152
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)
140154
call optional_int_to_string(legend_fontsize, legend_fontsize_str, default_font_size_str)
141155

142156
me%str = ''
@@ -146,6 +160,7 @@ subroutine initialize(me, grid, xlabel, ylabel, title, legend, use_numpy, figsiz
146160

147161
call me%add_str('import matplotlib')
148162
call me%add_str('import matplotlib.pyplot as plt')
163+
if (me%mplot3d) call me%add_str('from mpl_toolkits.mplot3d import Axes3D')
149164
if (me%use_numpy) call me%add_str('import numpy as np')
150165
call me%add_str('')
151166

@@ -163,16 +178,22 @@ subroutine initialize(me, grid, xlabel, ylabel, title, legend, use_numpy, figsiz
163178
else
164179
call me%add_str('fig = plt.figure()')
165180
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+
168188
if (present(grid)) then
169189
if (grid) call me%add_str('ax.grid()')
170190
end if
171191

172192
if (present(xlabel)) call me%add_str('ax.set_xlabel("'//trim(xlabel)//'")')
173193
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)//'")')
174195
if (present(title)) call me%add_str('ax.set_title("' //trim(title) //'")')
175-
196+
176197
call me%add_str('')
177198

178199
end subroutine initialize
@@ -232,6 +253,68 @@ subroutine add_plot(me, x, y, label, linestyle, markersize, linewidth)
232253
end subroutine add_plot
233254
!*****************************************************************************************
234255

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+
235318
!*****************************************************************************************
236319
!> author: Jacob Williams
237320
!

0 commit comments

Comments
 (0)