Skip to content

Commit 3c3bc40

Browse files
Merge pull request #41 from huijunchen9260/master
add wireframe based on plot_surface
2 parents b98f9fe + 3e64e05 commit 3c3bc40

File tree

1 file changed

+92
-1
lines changed

1 file changed

+92
-1
lines changed

src/pyplot_module.F90

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
module pyplot_module
2626

27-
use, intrinsic :: iso_fortran_env
27+
use, intrinsic :: iso_fortran_env
2828

2929
implicit none
3030

@@ -83,6 +83,7 @@ module pyplot_module
8383
procedure, public :: add_3d_plot !! add a 3d plot to pyplot instance
8484
procedure, public :: add_sphere !! add a 3d sphere to pyplot instance
8585
procedure, public :: add_contour !! add a contour plot to pyplot instance
86+
procedure, public :: plot_wireframe!! add a wireframe plot to pyplot instance
8687
procedure, public :: plot_surface !! add a surface plot to pyplot instance
8788
procedure, public :: add_bar !! add a barplot to pyplot instance
8889
procedure, public :: add_imshow !! add an image plot (using `imshow`)
@@ -656,6 +657,96 @@ subroutine plot_surface(me, x, y, z, label, linestyle, linewidth, levels, color,
656657
end subroutine plot_surface
657658
!*****************************************************************************************
658659

660+
!*****************************************************************************************
661+
!> author: Jacob Williams
662+
!
663+
! Add a wireframe plot.
664+
!
665+
!@note This requires `use_numpy` to be True.
666+
667+
subroutine plot_wireframe(me, x, y, z, label, linestyle, linewidth, levels, color, &
668+
cmap, colorbar, antialiased, istat)
669+
670+
class(pyplot), intent (inout) :: me !! pyplot handler
671+
real(wp),dimension(:), intent (in) :: x !! x values
672+
real(wp),dimension(:), intent (in) :: y !! y values
673+
real(wp),dimension(:,:), intent (in) :: z !! z values (a matrix)
674+
character(len=*), intent (in) :: label !! plot label
675+
character(len=*), intent (in) :: linestyle !! style of the plot line
676+
integer, intent (in), optional :: linewidth !! width of the plot line
677+
real(wp),dimension(:), intent (in), optional :: levels !! contour levels to plot
678+
character(len=*), intent (in), optional :: color !! Color of the surface patches
679+
character(len=*), intent (in), optional :: cmap !! colormap if filled=True (examples: 'jet', 'bone')
680+
logical, intent (in), optional :: colorbar !! add a colorbar (default=False)
681+
logical, intent (in), optional :: antialiased !! The surface is made opaque by using antialiased=False
682+
integer, intent (out), optional :: istat !! status output (0 means no problems)
683+
684+
character(len=:), allocatable :: xstr !! x values stringified
685+
character(len=:), allocatable :: ystr !! y values stringified
686+
character(len=:), allocatable :: zstr !! z values stringified
687+
character(len=:), allocatable :: levelstr !! levels vector stringified
688+
character(len=:), allocatable :: antialiasedstr !! antialiased stringified
689+
character(len=max_int_len) :: iline !! actual line width
690+
character(len=*), parameter :: xname = 'x' !! x variable name for script
691+
character(len=*), parameter :: yname = 'y' !! y variable name for script
692+
character(len=*), parameter :: zname = 'z' !! z variable name for script
693+
character(len=*), parameter :: xname_ = 'X' !! X variable name for contour
694+
character(len=*), parameter :: yname_ = 'Y' !! Y variable name for contour
695+
character(len=*), parameter :: zname_ = 'Z' !! Z variable name for contour
696+
character(len=:), allocatable :: extras !! optional stuff
697+
698+
if (allocated(me%str)) then
699+
700+
if (present(istat)) istat = 0
701+
702+
!convert the arrays to strings:
703+
call vec_to_string(x, me%real_fmt, xstr, me%use_numpy)
704+
call vec_to_string(y, me%real_fmt, ystr, me%use_numpy)
705+
call matrix_to_string(z, me%real_fmt, zstr, me%use_numpy)
706+
if (present(levels)) call vec_to_string(levels, me%real_fmt, levelstr, me%use_numpy)
707+
708+
!get optional inputs (if not present, set default value):
709+
call optional_int_to_string(linewidth, iline, '3')
710+
call optional_logical_to_string(antialiased, antialiasedstr, 'False')
711+
712+
!write the arrays:
713+
call me%add_str(trim(xname)//' = '//xstr)
714+
call me%add_str(trim(yname)//' = '//ystr)
715+
call me%add_str(trim(zname)//' = '//zstr)
716+
call me%add_str('')
717+
718+
!convert inputs for contour plotting:
719+
call me%add_str(xname_//', '//yname_//' = np.meshgrid('//trim(xname)//', '//trim(yname)//')')
720+
call me%add_str(zname_//' = np.transpose('//zname//')')
721+
722+
!optional arguments:
723+
extras = ''
724+
if (present(levels)) extras = extras//','//'levels='//levelstr
725+
if (present(color)) extras = extras//','//'colors='//trim(me%raw_str_token)//'"'//color//'"'
726+
if (present(linewidth)) extras = extras//','//'linewidths='//trim(adjustl(iline))
727+
if (present(cmap)) extras = extras//','//'cmap='//trim(me%raw_str_token)//'"'//cmap//'"'
728+
729+
!write the plot statement:
730+
call me%add_str('CS = ax.plot_wireframe'//'('//xname_//','//yname_//','//zname_//','//&
731+
'label='//trim(me%raw_str_token)//'"'//trim(label)//'",'//&
732+
'antialiased='//trim(antialiasedstr)//','//&
733+
'linestyles='//trim(me%raw_str_token)//'"'//trim(adjustl(linestyle))//'"'//&
734+
extras//')')
735+
736+
if (present(colorbar)) then
737+
if (colorbar) call me%add_str('fig.colorbar(CS)')
738+
end if
739+
740+
call me%add_str('')
741+
742+
else
743+
if (present(istat)) istat = -1
744+
write(error_unit,'(A)') 'Error in add_plot: pyplot class not properly initialized.'
745+
end if
746+
747+
end subroutine plot_wireframe
748+
!*****************************************************************************************
749+
659750
!*****************************************************************************************
660751
!> author: Jacob Williams
661752
!

0 commit comments

Comments
 (0)