Skip to content

Commit d9ded3f

Browse files
Merge pull request #49 from jacobwilliams/develop
updates
2 parents a362b18 + a8ad996 commit d9ded3f

File tree

4 files changed

+324
-250
lines changed

4 files changed

+324
-250
lines changed

.gitignore

+42-41
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,42 @@
1-
bin/
2-
lib/
3-
doc/
4-
build/
5-
__pycache__
6-
7-
# Compiled Object files
8-
*.slo
9-
*.lo
10-
*.o
11-
*.obj
12-
13-
# Precompiled Headers
14-
*.gch
15-
*.pch
16-
17-
# Compiled Dynamic libraries
18-
*.so
19-
*.dylib
20-
*.dll
21-
22-
# Fortran module files
23-
*.mod
24-
25-
# Compiled Static libraries
26-
*.lai
27-
*.la
28-
*.a
29-
*.lib
30-
31-
# Executables
32-
*.exe
33-
*.out
34-
*.app
35-
36-
# Generated Test Files
37-
test/*.png
38-
test/*.py
39-
40-
# misc
41-
.DS_Store
1+
bin/
2+
lib/
3+
doc/
4+
build/
5+
__pycache__
6+
7+
# Compiled Object files
8+
*.slo
9+
*.lo
10+
*.o
11+
*.obj
12+
13+
# Precompiled Headers
14+
*.gch
15+
*.pch
16+
17+
# Compiled Dynamic libraries
18+
*.so
19+
*.dylib
20+
*.dll
21+
22+
# Fortran module files
23+
*.mod
24+
25+
# Compiled Static libraries
26+
*.lai
27+
*.la
28+
*.a
29+
*.lib
30+
31+
# Executables
32+
*.exe
33+
*.out
34+
*.app
35+
36+
# Generated Test Files
37+
test/*.png
38+
test/*.py
39+
40+
# misc
41+
.DS_Store
42+
/env

src/pyplot_module.F90

+27-4
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ module pyplot_module
7171
logical :: tight_layout = .false. !! tight layout option
7272
logical :: usetex = .false. !! enable LaTeX
7373

74+
character(len=:),allocatable :: xaxis_date_fmt !! date format for the x-axis. Example: `"%m/%d/%y %H:%M:%S"`
75+
character(len=:),allocatable :: yaxis_date_fmt !! date format for the y-axis. Example: `"%m/%d/%y %H:%M:%S"`
76+
7477
character(len=:),allocatable :: real_fmt !! real number formatting
7578

7679
contains
@@ -162,7 +165,7 @@ end subroutine add_str
162165
subroutine initialize(me, grid, xlabel, ylabel, zlabel, title, legend, use_numpy, figsize, &
163166
font_size, axes_labelsize, xtick_labelsize, ytick_labelsize, ztick_labelsize, &
164167
legend_fontsize, mplot3d, axis_equal, polar, real_fmt, use_oo_api, axisbelow,&
165-
tight_layout, raw_strings, usetex)
168+
tight_layout, raw_strings, usetex, xaxis_date_fmt, yaxis_date_fmt)
166169

167170
class(pyplot), intent(inout) :: me !! pyplot handler
168171
logical, intent(in), optional :: grid !! activate grid drawing
@@ -189,6 +192,8 @@ subroutine initialize(me, grid, xlabel, ylabel, zlabel, title, legend, use_numpy
189192
logical, intent(in), optional :: raw_strings !! if True, all strings sent to Python are treated as
190193
!! raw strings (e.g., r'str'). Default is False.
191194
logical, intent(in), optional :: usetex !! if True, enable LaTeX. (default if false)
195+
character(len=*), intent(in), optional :: xaxis_date_fmt !! if present, used to set the date format for the x-axis
196+
character(len=*), intent(in), optional :: yaxis_date_fmt !! if present, used to set the date format for the y-axis
192197

193198
character(len=max_int_len) :: width_str !! figure width dummy string
194199
character(len=max_int_len) :: height_str !! figure height dummy string
@@ -257,6 +262,16 @@ subroutine initialize(me, grid, xlabel, ylabel, zlabel, title, legend, use_numpy
257262
else
258263
me%usetex = .false.
259264
end if
265+
if (present(xaxis_date_fmt)) then
266+
me%xaxis_date_fmt = xaxis_date_fmt
267+
else
268+
if (allocated(me%xaxis_date_fmt)) deallocate(me%xaxis_date_fmt)
269+
end if
270+
if (present(yaxis_date_fmt)) then
271+
me%yaxis_date_fmt = yaxis_date_fmt
272+
else
273+
if (allocated(me%yaxis_date_fmt)) deallocate(me%yaxis_date_fmt)
274+
end if
260275

261276
call optional_int_to_string(font_size, font_size_str, default_font_size_str)
262277
call optional_int_to_string(axes_labelsize, axes_labelsize_str, default_font_size_str)
@@ -1401,11 +1416,11 @@ subroutine execute(me, pyfile, istat, python)
14011416
write(error_unit,'(A)') 'Error closing file: '//trim(file)
14021417
else
14031418

1404-
if (present(python)) then
1419+
if (present(python)) then
14051420
python_ = trim(python)
1406-
else
1421+
else
14071422
python_ = python_exe
1408-
end if
1423+
end if
14091424

14101425
!run the file using python:
14111426
if (index(file,' ')>0) then
@@ -1477,6 +1492,14 @@ subroutine finish_ops(me)
14771492
end if
14781493
call me%add_str('')
14791494
end if
1495+
if (allocated(me%xaxis_date_fmt) .or. allocated(me%yaxis_date_fmt)) then
1496+
call me%add_str('from matplotlib.dates import DateFormatter')
1497+
if (allocated(me%xaxis_date_fmt)) &
1498+
call me%add_str('ax.xaxis.set_major_formatter(DateFormatter("'//trim(me%xaxis_date_fmt)//'"))')
1499+
if (allocated(me%yaxis_date_fmt)) &
1500+
call me%add_str('ax.yaxis.set_major_formatter(DateFormatter("'//trim(me%yaxis_date_fmt)//'"))')
1501+
call me%add_str('')
1502+
end if
14801503
if (me%tight_layout) then
14811504
call me%add_str('fig.tight_layout()')
14821505
call me%add_str('')

test/date_test.f90

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
!*****************************************************************************************
2+
!>
3+
! Unit test for [[pyplot_module]]. Using the `xaxis_date_fmt` option for x-axis dates.
4+
5+
program date_test
6+
7+
use pyplot_module, only : pyplot, wp => pyplot_wp
8+
9+
implicit none
10+
11+
integer,parameter :: n = 100
12+
13+
real(wp), dimension(:),allocatable :: x !! x values
14+
real(wp), dimension(:),allocatable :: y !! y values
15+
real(wp), dimension(:),allocatable :: sx !! sin(x) values
16+
real(wp), dimension(:),allocatable :: cx !! cos(x) values
17+
real(wp), dimension(:),allocatable :: tx !! sin(x)*cos(x) values
18+
type(pyplot) :: plt !! pytplot handler
19+
integer :: i !! counter
20+
integer :: istat !! status code
21+
22+
character(len=*), parameter :: testdir = "test/"
23+
character(len=*), parameter :: xaxis_date_fmt = '%m/%d/%y %H:%M:%S'
24+
character(len=*), parameter :: yaxis_date_fmt = '%H:%M:%S'
25+
26+
! size arrays:
27+
allocate(x(n))
28+
allocate(sx(n))
29+
allocate(cx(n))
30+
allocate(tx(n))
31+
32+
!generate some data:
33+
x = [(real(i,wp), i=0,size(x)-1)]/5.0_wp
34+
sx = 10*sin(x)
35+
cx = cos(x)
36+
tx = sx * cx
37+
38+
!2d line plot:
39+
call plt%initialize(grid=.true.,xlabel='Calendar date',figsize=[20,10],&
40+
title='date test',legend=.true.,axis_equal=.true.,&
41+
tight_layout=.true., &
42+
xaxis_date_fmt=xaxis_date_fmt, yaxis_date_fmt=yaxis_date_fmt)
43+
call plt%add_plot(x,sx,label='$\sin (x)$',linestyle='b-o',markersize=5,linewidth=2,istat=istat)
44+
call plt%add_plot(x,cx,label='$\cos (x)$',linestyle='r-o',markersize=5,linewidth=2,istat=istat)
45+
call plt%add_plot(x,tx,label='$\sin (x) \cos (x)$',linestyle='g-o',markersize=2,linewidth=1,istat=istat)
46+
call plt%savefig(testdir//'datetest.png', pyfile=testdir//'datetest.py',&
47+
istat=istat)
48+
49+
end program date_test
50+
!*****************************************************************************************

0 commit comments

Comments
 (0)