Self-Study Guide 2: Programming in Fortran 95 by Dr. Rachael Padman - HTML preview

PLEASE NOTE: This is an HTML preview only and some elements such as links or page numbers may be incorrect.
Download the book in PDF, ePub, Kindle for a complete version.

4 Graphics and Visualisation

There are many ways in which to plot data. It is very difficult to write graphics applications in F95, so generally it is easier (and better) to use an application. One such application is gnuplot, which is a free plotting program that can plot data files and user-defined functions. It can’t do everything you might possibly want, but it is very easy to use. We introduce gnuplot here – there is documentation available via ‘help’ within the program and on the course web site.

gnuplot is a command-line driven program. Typing gnuplot at the terminal you will see that the prompt changes. You will want to use the help command to find out more information. You will also be able to output graphs from gnuplot in a form you can import into, say, Microsoft Word, when you produce your report.

4.1 Plotting a data file

gnuplot expects data to be arranged in columns in an ordinary text file, e.g.

# Gnu population in Antarctica since 1965

         1965   103

         1970   55

         1975   34

         1980   24

         1985   10

You can have as many columns as you like. Comments are indicated by ‘#’. The recommended way you use gnuplot to produce results from your F95 program is therefore to write out results to a file and use gnuplot to plot them. Let’s look at a simple F95 program to do just that.

img28.png

The file data1.dat should contain the two columns of numbers, exactly the format needed by gnuplot. To plot the data is very easy:

  • Enter this program, compile and run it and produce the data file data1.dat.
  • Start up gnuplot.
  • Within gnuplot give the command:

plot ’data1.dat’

4.2 Getting help

You can get help by typing ‘?’ or ‘help’ within gnuplot. The on-line help is very good. You can also abbreviate commands to save typing.

4.3 Further examples

As well as plotting data we can plot functions in gnuplot.

  • For example to plot one of the trigonometric functions type the following: plot

sin(x)*cos(x)

  • In fact, gnuplot lets us define a function:

pop(x) = sin(x)*(1-cos(x/3.0))

  • Then we can plot this function, for 0 ≤ x ≤ 10, say, as follows:

plot [0:10] pop(x)

  • To plot the data file created using the F95 program of the previous section we can use:

plot ’data1.dat’

  • And you can also plot both the function and the data together:

plot [0:10] ’data1.dat’, pop(x)

  • By default, data files are plotted point by point. If you want lines joining the points:

plot ’data1.dat’ with linesp

  • If you want lines only:

plot ’data1.dat’ w lines

  • To control which colour each set of lines and points comes out, see help plot. For example, to make the data come out with colour 2 (dotted lines), and pop(x) with colour 1,

plot [0:10] ’data1.dat’ w lines 2 2, pop(x) \

w lines 1 1

  • The backslash enables you to continue on to another line if the commands become long.
  • To plot column 4 of ‘flib.dat’ against column 2 of the same file:

plot ’flib.dat’ u 2:4 w linesp

  • this gives column 2 on the x axis and 4 on the y axis. You can also plot points with error bars. The following command plots column 4 versus column 2, with columns 5 and 6 defining the upper and lower error bars:

plot ’flib.dat’ u 2:4:5:6 w errorbars

4.4 Printing graphs into PostScript files

  • The following sequence changes the terminal type to PostScript and replots the most recent plot to a file called file.ps:

set term post

set output ’file.ps’

replot

  • Don’t forget to set the terminal type back to X11 when you are done plotting to the file.

set term X

  • In order to get graphs that are readable when included in papers, I recommend: set size 0.6,0.6

before plotting to the file. This reduces the size of the graph while keeping the font size and point size constant.

There are other output types from gnuplot. In particular you may want to use the CGM terminal type instead of the PostScript terminal type as above (use cgm instead of post). This produces a file which can be read directly into Microsoft Word and converted to a Word drawing.

img29.png