Skip to content

Commit 2614fbc

Browse files
committed
added plot_surface. Fixes #24
1 parent 1c98108 commit 2614fbc

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

src/pyplot_module.f90

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ module pyplot_module
5656
procedure, public :: add_3d_plot !! add a 3d plot to pyplot instance
5757
procedure, public :: add_sphere !! add a 3d sphere to pyplot instance
5858
procedure, public :: add_contour !! add a contour plot to pyplot instance
59+
procedure, public :: plot_surface !! add a surface plot to pyplot instance
5960
procedure, public :: add_bar !! add a barplot to pyplot instance
6061
procedure, public :: add_imshow !! add an image plot (using `imshow`)
6162
procedure, public :: add_hist !! add a histogram plot to pyplot instance
@@ -528,6 +529,96 @@ subroutine add_contour(me, x, y, z, label, linestyle, linewidth, levels, color,
528529
end subroutine add_contour
529530
!*****************************************************************************************
530531

532+
!*****************************************************************************************
533+
!> author: Jacob Williams
534+
!
535+
! Add a surface plot.
536+
!
537+
!@note This requires `use_numpy` to be True.
538+
539+
subroutine plot_surface(me, x, y, z, label, linestyle, linewidth, levels, color, &
540+
cmap, colorbar, antialiased, istat)
541+
542+
class(pyplot), intent (inout) :: me !! pyplot handler
543+
real(wp),dimension(:), intent (in) :: x !! x values
544+
real(wp),dimension(:), intent (in) :: y !! y values
545+
real(wp),dimension(:,:), intent (in) :: z !! z values (a matrix)
546+
character(len=*), intent (in) :: label !! plot label
547+
character(len=*), intent (in) :: linestyle !! style of the plot line
548+
integer, intent (in), optional :: linewidth !! width of the plot line
549+
real(wp),dimension(:), intent (in), optional :: levels !! contour levels to plot
550+
character(len=*), intent (in), optional :: color !! Color of the surface patches
551+
character(len=*), intent (in), optional :: cmap !! colormap if filled=True (examples: 'jet', 'bone')
552+
logical, intent (in), optional :: colorbar !! add a colorbar (default=False)
553+
logical, intent (in), optional :: antialiased !! The surface is made opaque by using antialiased=False
554+
integer, intent (out) :: istat !! status output (0 means no problems)
555+
556+
character(len=:), allocatable :: xstr !! x values stringified
557+
character(len=:), allocatable :: ystr !! y values stringified
558+
character(len=:), allocatable :: zstr !! z values stringified
559+
character(len=:), allocatable :: levelstr !! levels vector stringified
560+
character(len=:), allocatable :: antialiasedstr !! antialiased stringified
561+
character(len=max_int_len) :: iline !! actual line width
562+
character(len=*), parameter :: xname = 'x' !! x variable name for script
563+
character(len=*), parameter :: yname = 'y' !! y variable name for script
564+
character(len=*), parameter :: zname = 'z' !! z variable name for script
565+
character(len=*), parameter :: xname_ = 'X' !! X variable name for contour
566+
character(len=*), parameter :: yname_ = 'Y' !! Y variable name for contour
567+
character(len=*), parameter :: zname_ = 'Z' !! Z variable name for contour
568+
character(len=:), allocatable :: extras !! optional stuff
569+
570+
if (allocated(me%str)) then
571+
572+
istat = 0
573+
574+
!convert the arrays to strings:
575+
call vec_to_string(x, me%real_fmt, xstr, me%use_numpy)
576+
call vec_to_string(y, me%real_fmt, ystr, me%use_numpy)
577+
call matrix_to_string(z, me%real_fmt, zstr, me%use_numpy)
578+
if (present(levels)) call vec_to_string(levels, me%real_fmt, levelstr, me%use_numpy)
579+
580+
!get optional inputs (if not present, set default value):
581+
call optional_int_to_string(linewidth, iline, '3')
582+
call optional_logical_to_string(antialiased, antialiasedstr, 'False')
583+
584+
!write the arrays:
585+
call me%add_str(trim(xname)//' = '//xstr)
586+
call me%add_str(trim(yname)//' = '//ystr)
587+
call me%add_str(trim(zname)//' = '//zstr)
588+
call me%add_str('')
589+
590+
!convert inputs for contour plotting:
591+
call me%add_str(xname_//', '//yname_//' = np.meshgrid('//trim(xname)//', '//trim(yname)//')')
592+
call me%add_str(zname_//' = np.transpose('//zname//')')
593+
594+
!optional arguments:
595+
extras = ''
596+
if (present(levels)) extras = extras//','//'levels='//levelstr
597+
if (present(color)) extras = extras//','//'colors="'//color//'"'
598+
if (present(linewidth)) extras = extras//','//'linewidths='//trim(adjustl(iline))
599+
if (present(cmap)) extras = extras//','//'cmap="'//cmap//'"'
600+
601+
!write the plot statement:
602+
call me%add_str('CS = ax.plot_surface'//'('//xname_//','//yname_//','//zname_//','//&
603+
'label="'//trim(label)//'",'//&
604+
'antialiased='//trim(antialiasedstr)//','//&
605+
'linestyles="'//trim(adjustl(linestyle))//'"'//&
606+
extras//')')
607+
608+
if (present(colorbar)) then
609+
if (colorbar) call me%add_str('fig.colorbar(CS)')
610+
end if
611+
612+
call me%add_str('')
613+
614+
else
615+
istat = -1
616+
write(error_unit,'(A)') 'Error in add_plot: pyplot class not properly initialized.'
617+
end if
618+
619+
end subroutine plot_surface
620+
!*****************************************************************************************
621+
531622
!*****************************************************************************************
532623
!> author: Jacob Williams
533624
!

0 commit comments

Comments
 (0)