Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
Fixed a test with latest matplotlib. Fixes #47
Added a new option to use date formatting for the axes.
  • Loading branch information
jacobwilliams committed Mar 27, 2024
1 parent a362b18 commit a8ad996
Show file tree
Hide file tree
Showing 4 changed files with 324 additions and 250 deletions.
83 changes: 42 additions & 41 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,41 +1,42 @@
bin/
lib/
doc/
build/
__pycache__

# Compiled Object files
*.slo
*.lo
*.o
*.obj

# Precompiled Headers
*.gch
*.pch

# Compiled Dynamic libraries
*.so
*.dylib
*.dll

# Fortran module files
*.mod

# Compiled Static libraries
*.lai
*.la
*.a
*.lib

# Executables
*.exe
*.out
*.app

# Generated Test Files
test/*.png
test/*.py

# misc
.DS_Store
bin/
lib/
doc/
build/
__pycache__

# Compiled Object files
*.slo
*.lo
*.o
*.obj

# Precompiled Headers
*.gch
*.pch

# Compiled Dynamic libraries
*.so
*.dylib
*.dll

# Fortran module files
*.mod

# Compiled Static libraries
*.lai
*.la
*.a
*.lib

# Executables
*.exe
*.out
*.app

# Generated Test Files
test/*.png
test/*.py

# misc
.DS_Store
/env
31 changes: 27 additions & 4 deletions src/pyplot_module.F90
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ module pyplot_module
logical :: tight_layout = .false. !! tight layout option
logical :: usetex = .false. !! enable LaTeX

character(len=:),allocatable :: xaxis_date_fmt !! date format for the x-axis. Example: `"%m/%d/%y %H:%M:%S"`
character(len=:),allocatable :: yaxis_date_fmt !! date format for the y-axis. Example: `"%m/%d/%y %H:%M:%S"`

character(len=:),allocatable :: real_fmt !! real number formatting

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

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

character(len=max_int_len) :: width_str !! figure width dummy string
character(len=max_int_len) :: height_str !! figure height dummy string
Expand Down Expand Up @@ -257,6 +262,16 @@ subroutine initialize(me, grid, xlabel, ylabel, zlabel, title, legend, use_numpy
else
me%usetex = .false.
end if
if (present(xaxis_date_fmt)) then
me%xaxis_date_fmt = xaxis_date_fmt
else
if (allocated(me%xaxis_date_fmt)) deallocate(me%xaxis_date_fmt)
end if
if (present(yaxis_date_fmt)) then
me%yaxis_date_fmt = yaxis_date_fmt
else
if (allocated(me%yaxis_date_fmt)) deallocate(me%yaxis_date_fmt)
end if

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

if (present(python)) then
if (present(python)) then
python_ = trim(python)
else
else
python_ = python_exe
end if
end if

!run the file using python:
if (index(file,' ')>0) then
Expand Down Expand Up @@ -1477,6 +1492,14 @@ subroutine finish_ops(me)
end if
call me%add_str('')
end if
if (allocated(me%xaxis_date_fmt) .or. allocated(me%yaxis_date_fmt)) then
call me%add_str('from matplotlib.dates import DateFormatter')
if (allocated(me%xaxis_date_fmt)) &
call me%add_str('ax.xaxis.set_major_formatter(DateFormatter("'//trim(me%xaxis_date_fmt)//'"))')
if (allocated(me%yaxis_date_fmt)) &
call me%add_str('ax.yaxis.set_major_formatter(DateFormatter("'//trim(me%yaxis_date_fmt)//'"))')
call me%add_str('')
end if
if (me%tight_layout) then
call me%add_str('fig.tight_layout()')
call me%add_str('')
Expand Down
50 changes: 50 additions & 0 deletions test/date_test.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
!*****************************************************************************************
!>
! Unit test for [[pyplot_module]]. Using the `xaxis_date_fmt` option for x-axis dates.

program date_test

use pyplot_module, only : pyplot, wp => pyplot_wp

implicit none

integer,parameter :: n = 100

real(wp), dimension(:),allocatable :: x !! x values
real(wp), dimension(:),allocatable :: y !! y values
real(wp), dimension(:),allocatable :: sx !! sin(x) values
real(wp), dimension(:),allocatable :: cx !! cos(x) values
real(wp), dimension(:),allocatable :: tx !! sin(x)*cos(x) values
type(pyplot) :: plt !! pytplot handler
integer :: i !! counter
integer :: istat !! status code

character(len=*), parameter :: testdir = "test/"
character(len=*), parameter :: xaxis_date_fmt = '%m/%d/%y %H:%M:%S'
character(len=*), parameter :: yaxis_date_fmt = '%H:%M:%S'

! size arrays:
allocate(x(n))
allocate(sx(n))
allocate(cx(n))
allocate(tx(n))

!generate some data:
x = [(real(i,wp), i=0,size(x)-1)]/5.0_wp
sx = 10*sin(x)
cx = cos(x)
tx = sx * cx

!2d line plot:
call plt%initialize(grid=.true.,xlabel='Calendar date',figsize=[20,10],&
title='date test',legend=.true.,axis_equal=.true.,&
tight_layout=.true., &
xaxis_date_fmt=xaxis_date_fmt, yaxis_date_fmt=yaxis_date_fmt)
call plt%add_plot(x,sx,label='$\sin (x)$',linestyle='b-o',markersize=5,linewidth=2,istat=istat)
call plt%add_plot(x,cx,label='$\cos (x)$',linestyle='r-o',markersize=5,linewidth=2,istat=istat)
call plt%add_plot(x,tx,label='$\sin (x) \cos (x)$',linestyle='g-o',markersize=2,linewidth=1,istat=istat)
call plt%savefig(testdir//'datetest.png', pyfile=testdir//'datetest.py',&
istat=istat)

end program date_test
!*****************************************************************************************
Loading

0 comments on commit a8ad996

Please sign in to comment.