How great would it be if we can have a plotting option directly in Fortran programs? I was having trouble finding a proper way to do this. Then I came across the wonderful gnuplot software (free and open source !!). It is a command line plotting utility that can produce publication-quality plots. So what has it got to do with Fortran programs? Since gnuplot can be run from a terminal, we can ask the Fortran program to open a system terminal and ask to run gnuplot. Once gnuplot finishes plotting (to your display/ or to a file), then the control will be given back to the Fortran program to continue execution.
Here is an example Fortran program:
PROGRAM compute_plot INTEGER :: i,n=10 REAL :: x(10),y(10) x(1)=0.0 y(1)=0.0 DO i=2,n x(i)=0.1*i y(i)=x(i)*x(i) ENDDO OPEN(UNIT=48,FILE='data') DO i=1,n WRITE(48,*) x(i),y(i) ENDDO CLOSE(48) CALL SYSTEM('gnuplot -p data_plot.plt') END PROGRAM compute_plot
The above Fortran program actually calculates the points on the parabola y=x^2 and then writes into the file data. gnuplot is then called to run a file named data_plot.plt which is located in the same folder as the Fortran program. The gnuplot file data_plot.plt is listed below.
set xlabel "x" set ylabel "y" m="./data" set terminal x11 0 set nokey set grid set title 'The parabola' plot m using 1:2 with linespoints
The following plot is displayed when the Fortran program is executed.
If you are running Ubuntu, gnuplot can be installed from terminal by the command:
$ sudo apt-get install gnuplot
For other platforms, please visit: http://www.gnuplot.info/
9 thoughts on “gnuplot in Fortran”
hi, just want to kknow if the gnuplot command will work in the exe file made from fortran? i wish to make a exe program which need to do some plotting. this program has to be shared across windows pc’s , so wish to know if the gnu plot command will work on pc’s which doesnt have gnuplot
It will not work if the other system does not have gnuplot. Only if the command ‘gnuplot -p data_plot.plt’ works independently in a system, then the fortran executable will be able to plot. Also, data_plot.plt file must be present in the execuable’s directory.
By the way, I am not a windows user. I am assuming that ‘gnuplot -p data_plot.plt’ is the right command to call gnuplot routines from windows command prompt. If that is not the correct command there, you may need to modify that as well.
It would be more easy If you try:
#write(64,*)”set xlabel “x””
#CALL SYSTEM(‘gnuplot -p data_plot.plt’)
it will take one enter to plot the data
The post shows a simple example for plotting. Usually, the need is to plot many quantities from the data in various ways. So the gnuplot .plt file will easily turn out to be a lengthy file. Also, this post was only a suggestion to show that such a connection can be made to an external program from Fortran. Any other visualization program (like Python-Matplotlib) can be called in this way.
Thanks for your comment!
Again Some query , The gnuplot window is static here . I can’t operate it manually by mouse , I mean , I can’t rotate the figures etc in the plot window with mouse . Where is the problem ?
ok , That is too solved by writing “pause -1” in end of gnuplot file .
I execute your example program from visual Studio 2008, compiled with INTEL. When I execute it, the message ‘gnuplot is not recognized as an internal or external command, program or batch file’ appears. Nevertheless I can call gnuplot from the windows command window.
Do you have an idea what could be the problem?
Is it problematic, that I execute the program form storage device E, while gnuplot and windows are installed on C.
I am not a windows user. I am guessing that the issue is not one of path, like things are installed in C drive and so on. But may be the .exe files can not simply call another program in windows without enough permissions. This is only my guess. So please check whether any other program like notepad is callable via the ‘CALL SYSTEM ( )’ approach.
You just have to open the executable file, after compilation, in the debug/release paste. It is quite simple.