Skip to content

Commit e3f299f

Browse files
committed
added bar charts.
1 parent b67c738 commit e3f299f

File tree

3 files changed

+82
-5
lines changed

3 files changed

+82
-5
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,19 @@ A simple module for generating plots from Fortran using Python's matplotlib.pypl
66
Overview
77
---------------
88

9-
Currently, this module can be used to generate simple x-y plots from Fortran. Eventually, it may be expanded to provide additional features and other types of plots.
9+
Currently, this module can be used to generate simple plots from Fortran. Eventually, it may be expanded to provide additional features and other types of plots.
1010

1111
The way it works is simply to generate a Python script with the plotting code, which
1212
is then executed from the command line using the Fortran ```execute_command_line``` function.
1313

1414
The module requires a modern Fortran compiler (it uses various Fortran 2003/2008 features such as deferred-length strings). It should work fine with the latest gfortran or ifort compilers. A simple script ```build.sh``` is provided for building the library and test program (requires gfortran and [FoBiS](https://github.com/szaghi/FoBiS)). It will also build the HTML documentation if [ROBODoc](https://github.com/gumpu/ROBODoc) is installed.
1515

16+
Supported plot types
17+
---------------
18+
19+
* ```matplotlib.pyplot.plot``` -- 2D plot of lines and/or markers.
20+
* ```matplotlib.pyplot.bar``` -- Make a bar plot.
21+
1622
Example
1723
---------------
1824

src/pyplot_module.f90

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ module pyplot_module
8989

9090
procedure,public :: initialize
9191
procedure,public :: add_plot
92+
procedure,public :: add_bar
9293
procedure,public :: savefig
9394
procedure,public :: destroy
9495

@@ -220,7 +221,7 @@ end subroutine initialize
220221
! add_plot
221222
!
222223
! DESCRIPTION
223-
! Add data for plotting.
224+
! Add an x,y plot.
224225
!
225226
! SOURCE
226227

@@ -272,6 +273,69 @@ subroutine add_plot(me,x,y,label,linestyle,markersize,linewidth)
272273
end subroutine add_plot
273274
!*****************************************************************************************
274275

276+
!*****************************************************************************************
277+
!****f* pyplot_module/add_bar
278+
!
279+
! NAME
280+
! add_bar
281+
!
282+
! DESCRIPTION
283+
! Add a bar plot.
284+
!
285+
! SOURCE
286+
287+
subroutine add_bar(me,left,height,label,width,bottom)
288+
289+
implicit none
290+
291+
class(pyplot),intent(inout) :: me
292+
real(wp),dimension(:),intent(in) :: left
293+
real(wp),dimension(:),intent(in) :: height
294+
character(len=*),intent(in) :: label
295+
real(wp),dimension(:),intent(in),optional :: width
296+
real(wp),dimension(:),intent(in),optional :: bottom
297+
298+
character(len=:),allocatable :: xstr,ystr,wstr,bstr,plt_str
299+
300+
character(len=*),parameter :: xname = 'x' !variable names for script
301+
character(len=*),parameter :: yname = 'y' !
302+
character(len=*),parameter :: wname = 'w' !
303+
character(len=*),parameter :: bname = 'b' !
304+
305+
!convert the arrays to strings:
306+
call vec_to_string(left,xstr)
307+
call vec_to_string(height,ystr)
308+
if (present(width)) call vec_to_string(width,wstr)
309+
if (present(bottom)) call vec_to_string(bottom,bstr)
310+
311+
!write the arrays:
312+
call me%add_str('')
313+
if (me%use_numpy) then
314+
call me%add_str(trim(xname)//' = np.array('//xstr//')')
315+
call me%add_str(trim(yname)//' = np.array('//ystr//')')
316+
if (present(width)) call me%add_str(trim(wname)//' = np.array('//wstr//')')
317+
if (present(bottom)) call me%add_str(trim(bname)//' = np.array('//bstr//')')
318+
else
319+
call me%add_str(trim(xname)//' = '//xstr)
320+
call me%add_str(trim(yname)//' = '//ystr)
321+
if (present(width)) call me%add_str(trim(wname)//' = '//wstr)
322+
if (present(bottom)) call me%add_str(trim(bname)//' = '//bstr)
323+
end if
324+
325+
!create the plot string:
326+
plt_str = 'ax.bar('//&
327+
'left='//trim(xname)//','//&
328+
'height='//trim(yname)//','
329+
if (present(width)) plt_str=plt_str//'width='//trim(wname)//','
330+
if (present(bottom)) plt_str=plt_str//'bottom='//trim(bstr)//','
331+
plt_str=plt_str//'label="'//trim(label)//'")'
332+
333+
!write the plot statement:
334+
call me%add_str(plt_str)
335+
336+
end subroutine add_bar
337+
!*****************************************************************************************
338+
275339
!*****************************************************************************************
276340
!****f* pyplot_module/optional_int_to_string
277341
!

src/test.f90

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,20 @@ program test
2929
cx = cos(x)
3030
tx = sx * cx
3131

32-
!plot it:
32+
!2d line plot:
3333
call plt%initialize(grid=.true.,xlabel='angle (rad)',&
34-
title='python_plot test',legend=.true.)
34+
title='plot test',legend=.true.)
3535
call plt%add_plot(x,sx,label='$\sin (x)$',linestyle='b-o',markersize=5,linewidth=2)
3636
call plt%add_plot(x,cx,label='$\cos (x)$',linestyle='r-o',markersize=5,linewidth=2)
3737
call plt%add_plot(x,tx,label='$\sin (x) \cos (x)$',linestyle='g-o',markersize=2,linewidth=1)
38-
call plt%savefig('test.png', pyfile='test.py')
38+
call plt%savefig('plottest.png', pyfile='plottest.py')
39+
40+
!bar chart:
41+
tx = 0.1_wp !for bar width
42+
call plt%initialize(grid=.true.,xlabel='angle (rad)',&
43+
title='bar test',legend=.true.)
44+
call plt%add_bar(left=x,height=sx,width=tx,label='$\sin (x)$')
45+
call plt%savefig('bartest.png', pyfile='bartest.py')
3946

4047
end program test
4148
!*****************************************************************************************

0 commit comments

Comments
 (0)