Skip to content

Commit fd13449

Browse files
committed
Added basic imshow plot type.
Fixes #11
1 parent 88c1ce0 commit fd13449

File tree

3 files changed

+71
-3
lines changed

3 files changed

+71
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Supported plot types
2020
* ```matplotlib.pyplot.bar``` -- bar plot
2121
* ```matplotlib.pyplot.contour``` -- contour plot
2222
* ```matplotlib.pyplot.contourf``` -- filled contour plot
23+
* ```matplotlib.pyplot.imshow``` -- image plot
2324

2425
Example
2526
---------------

src/pyplot_module.f90

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ module pyplot_module
5353
procedure, public :: add_3d_plot !! add a 3d plot to pyplot instance
5454
procedure, public :: add_contour !! add a contour plot to pyplot instance
5555
procedure, public :: add_bar !! add a barplot to pyplot instance
56+
procedure, public :: add_imshow !! add an image plot (using `imshow`)
5657

5758
procedure, public :: savefig !! save plots of pyplot instance
5859
procedure, public :: destroy !! destroy pyplot instance
@@ -453,7 +454,8 @@ end subroutine add_3d_plot
453454
!
454455
! Add a bar plot.
455456

456-
subroutine add_bar(me, left, height, label, width, bottom, color, yerr, align, xlim, ylim, xscale, yscale)
457+
subroutine add_bar(me, left, height, label, width, bottom, color, &
458+
yerr, align, xlim, ylim, xscale, yscale)
457459

458460
class(pyplot), intent(inout) :: me !! pyplot handler
459461
real(wp), dimension(:), intent(in) :: left !! left bar values
@@ -535,6 +537,54 @@ subroutine add_bar(me, left, height, label, width, bottom, color, yerr, align, x
535537
end subroutine add_bar
536538
!*****************************************************************************************
537539

540+
!*****************************************************************************************
541+
!>
542+
! Add an image plot using `imshow`.
543+
!
544+
!### Note
545+
! * Based on code by Ricardo Torres, 4/2/2017.
546+
547+
subroutine add_imshow(me, x, xlim, ylim)
548+
549+
class(pyplot), intent (inout) :: me !! pyplot handler
550+
real(wp),dimension(:,:),intent (in) :: x !! x values
551+
real(wp),dimension(2), intent (in), optional :: xlim !! x-axis range
552+
real(wp),dimension(2), intent (in), optional :: ylim !! y-axis range
553+
554+
character(len=:), allocatable :: xstr !! x values stringified
555+
character(len=*), parameter :: xname = 'x' !! x variable name for script
556+
557+
!axis limits (optional):
558+
character(len=:), allocatable :: xlimstr !! xlim values stringified
559+
character(len=:), allocatable :: ylimstr !! ylim values stringified
560+
561+
if (allocated(me%str)) then
562+
563+
if (present(xlim)) call vec_to_string(xlim, me%real_fmt, xlimstr, me%use_numpy)
564+
if (present(ylim)) call vec_to_string(ylim, me%real_fmt, ylimstr, me%use_numpy)
565+
566+
!convert the arrays to strings:
567+
call matrix_to_string(x, me%real_fmt, xstr, me%use_numpy)
568+
569+
!write the arrays:
570+
call me%add_str(trim(xname)//' = '//xstr)
571+
call me%add_str('')
572+
573+
!write the plot statement:
574+
call me%add_str('ax.imshow('//trim(xname)//')')
575+
call me%add_str('')
576+
577+
!axis limits:
578+
if (allocated(xlimstr)) call me%add_str('ax.set_xlim('//xlimstr//')')
579+
if (allocated(ylimstr)) call me%add_str('ax.set_ylim('//ylimstr//')')
580+
581+
else
582+
error stop 'Error in add_imshow: pyplot class not properly initialized.'
583+
end if
584+
585+
end subroutine add_imshow
586+
!*****************************************************************************************
587+
538588
!*****************************************************************************************
539589
!> author: Jacob Williams
540590
!

src/tests/test.f90

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,21 @@ program test
2626
integer :: j !! counter
2727
real(wp) :: r2 !! temp variable
2828

29+
real(wp), dimension(n,n) :: mat !! image values
30+
2931
!generate some data:
3032
x = [(real(i,wp), i=0,size(x)-1)]/5.0_wp
3133
sx = sin(x)
3234
cx = cos(x)
3335
tx = sx * cx
3436
yerr = abs(sx*.25_wp)
3537

38+
do i=1,n
39+
do j=1,n
40+
mat(i,j) = sin(real(i,wp)*real(j,wp))
41+
end do
42+
end do
43+
3644
!2d line plot:
3745
call plt%initialize(grid=.true.,xlabel='angle (rad)',figsize=[20,10],&
3846
title='plot test',legend=.true.,axis_equal=.true.)
@@ -63,10 +71,19 @@ program test
6371
z(i,j) = sin(x(i))*cos(y(j))*sin(r2)/(1.0_wp+log(r2+1.0_wp))
6472
end do
6573
end do
66-
call plt%initialize(grid=.true.,xlabel='x angle (rad)',ylabel='y angle (rad)',figsize=[10,10],&
74+
call plt%initialize(grid=.true.,xlabel='x angle (rad)',&
75+
ylabel='y angle (rad)',figsize=[10,10],&
6776
title='Contour plot test', real_fmt='*')
68-
call plt%add_contour(x, y, z, label='contour', linestyle='-', linewidth=2, filled=.true., cmap='bone')
77+
call plt%add_contour(x, y, z, label='contour', linestyle='-', &
78+
linewidth=2, filled=.true., cmap='bone')
6979
call plt%savefig('contour.png',pyfile='contour.py')
7080

81+
!image plot:
82+
call plt%initialize(grid=.true.,xlabel='x',ylabel='y',figsize=[20,20],&
83+
title='imshow test',&
84+
real_fmt='(F9.3)')
85+
call plt%add_imshow(mat,xlim=[0.0_wp, 100.0_wp],ylim=[0.0_wp, 100.0_wp])
86+
call plt%savefig('imshow.png', pyfile='imshow.py')
87+
7188
end program test
7289
!*****************************************************************************************

0 commit comments

Comments
 (0)