Skip to content

Commit e502558

Browse files
committed
fixed contour axes and added colorbar option.
1 parent 0638ffd commit e502558

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

src/pyplot_module.f90

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,8 @@ end subroutine add_hist
437437
!
438438
!@note This requires `use_numpy` to be True.
439439

440-
subroutine add_contour(me, x, y, z, label, linestyle, linewidth, levels, color, filled, cmap, istat)
440+
subroutine add_contour(me, x, y, z, label, linestyle, linewidth, levels, color, &
441+
filled, cmap, colorbar, istat)
441442

442443
class(pyplot), intent (inout) :: me !! pyplot handler
443444
real(wp),dimension(:), intent (in) :: x !! x values
@@ -450,6 +451,7 @@ subroutine add_contour(me, x, y, z, label, linestyle, linewidth, levels, color,
450451
character(len=*), intent (in), optional :: color !! color of the contour line
451452
logical, intent (in), optional :: filled !! use filled control (default=False)
452453
character(len=*), intent (in), optional :: cmap !! colormap if filled=True (examples: 'jet', 'bone')
454+
logical, intent (in), optional :: colorbar !! add a colorbar (default=False)
453455
integer, intent (out) :: istat !! status output (0 means no problems)
454456

455457
character(len=:), allocatable :: xstr !! x values stringified
@@ -486,8 +488,8 @@ subroutine add_contour(me, x, y, z, label, linestyle, linewidth, levels, color,
486488
call me%add_str('')
487489

488490
!convert inputs for contour plotting:
489-
call me%add_str(yname_//', '//xname_//' = np.meshgrid('//trim(xname)//', '//trim(yname)//')')
490-
call me%add_str(zname_//' = '//zname)
491+
call me%add_str(xname_//', '//yname_//' = np.meshgrid('//trim(xname)//', '//trim(yname)//')')
492+
call me%add_str(zname_//' = np.transpose('//zname//')')
491493

492494
!optional arguments:
493495
extras = ''
@@ -508,6 +510,10 @@ subroutine add_contour(me, x, y, z, label, linestyle, linewidth, levels, color,
508510
'linestyles="'//trim(adjustl(linestyle))//'"'//&
509511
extras//')')
510512

513+
if (present(colorbar)) then
514+
if (colorbar) call me%add_str('fig.colorbar(CS)')
515+
end if
516+
511517
call me%add_str('ax.clabel(CS, fontsize=9, inline=1)')
512518
call me%add_str('')
513519

src/tests/test.f90

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ program test
8282
title='Contour plot test', real_fmt='*',&
8383
axisbelow=.false.)
8484
call plt%add_contour(x, y, z, label='contour', linestyle='-', &
85-
linewidth=2, filled=.true., cmap='bone',istat=istat)
85+
linewidth=2, filled=.true., cmap='bone', colorbar=.true.,&
86+
istat=istat)
8687
call plt%savefig('contour.png',pyfile='contour.py',istat=istat)
8788

8889
!image plot:

0 commit comments

Comments
 (0)