BIL101, Introduction to Computers  and 

Information Systems

Chapter 12

A Portable Scientific Visualization Program: GnuPlot

    This chapter is devoted to the hours of twelfth week.  The subject is mostly aimed at the scientific visualization.  Since GNUPlot is one of the most powerful and easily  accessible world wide used free scientific visualization   program we concern with it. GNUPlot are come with  almost all distributions of Linux Systems. It has  distributions for also Windows and some other  operating systems. This is the reason why we focus  on this program here.  

Prepared by

Metin Demiralp

Istanbul Technical University, Informatics Institute,

Maslak { 80626, Istanbul, Türkiye)

Version 0. 60

    GNUPlot is an interactive function plotting program.  It has interactive or batch modes. To invoke interactive  mode it is sufficient to give the gnuplot command  at the command prompt of linux. When this is done a prompt   appears and prompts the user to give gnuplot commands.  The commands of gnuplot may need parameters and arguments depending on what you want them to do. Gnuplot syntax is  case sensitive (commands and function names written in  lowercase are not the same as those written in capital  letters). You can abbreviate the command names as  long as the abbreviation is not ambiguous. Any number of  commands may appear on a line, separated by semicolons (;).  Strings are indicated with quotes. They may be either single  or double quotation marks.

    You may extend the gnuplot commands over several input  lines but for this purpose you must end each  line except the last one with a backslash (\).   The backslash must be the last character on each line. 

    Now we can start to plot functions. First we deal with the   library functions and their combinations. Consider the  following command  

    plot sin(x)

    This command plots the sine function via the  following display output.

1.jpg (8470 bytes)

    As can be seen the display range is selected  between -10 and 10 for the x axis and between -1 and 1 for the  y axis. These are default values for the plotting range of the  sine function. The default plotting range in vertical direction  may vary depending on the function. Whereas the horizontal default   range is always between -10 and 10. The default ranges can be  changed by the user. For example

    plot [-5:5] sin(x)

command selects the horizontal plotting range  between -5 and 5. The specification of the horizontal range  needs two numerical values for the beginning and the end   points. They must be separated by colon and enclosed by the  brackets. One or both of the numerical values can be skipped.  In that case either left or right side of the colon will  include nothing. If both of the numerical values is not given   then inside of the brackets contains nothing.  The display output of this command is given below

2.jpg (7666 bytes)

    The ticks on the horizontal axis for this  plot are positioned by gnuplot which has used an interval  whose length equals 2 between ticks. This can be changed.  For this we can modify the last example as follows

    set xticks 1

   plot [-5:5] sin(x)

where the interval between two consecutive  ticks is set to 1. The resulting display is as follows.

3.jpg (7982 bytes)

    If the ticks on the x--axis are not desired  then it is sufficient to use the command set xticks 0.  Since the distance between two consecutive ticks are set to  zero the ticks are accumulated at the leftmost bottom corner  of the frame. This means the nonexistence of the ticks on the   horizontal axis.

    The range for the vertical coordinates can also be set by the   user. For this purpose we can use an additional pair of brackets.  For example, the command 

    plot [-5:5] [-2:2] sin(x)

    produces the following display output.

4.jpg (13938 bytes)

On the other hand the vertical tics can be controlled by using  the set ytics command as done in the following sample  command. 

    set ytics 1

    plot [-5:5] [-2:2] sin(x)

This produces the following display output. 

5.jpg (6911 bytes)

It is possible to plot more than one function at a single  attempt. For this purpose the functions to be plotted must  be given in comma separated form like the following command. 

    set xtics 1; set ytics 1

    plot [-5:5] [-2:2] sin(x), cos(x), tan(x)

    This plots the trigonometric functions, sine,  cosine, and tangent and produces the following display output.

6.jpg (16284 bytes)

    Now we can mention about some other features of GNUPlot. 

Comment

    In GNUPlot comments are created by using the character \#.  This symbol can be positioned at the beginning of or somewhere  else in a line. GNUPlot ignores the remaining portion of  the line after this character. The effect of this character   vanish between quotation marks, inside numbers and inside command   substitutions. As a matter of fact it is alive anywhere it makes  sense to work. 

Exit

    The commands exit and quitand the END-OF-FILE  character exit GNUPlot. All these commands clear the output  device before exiting. 

Functions

    The functions in GNUPlot are the same as the corresponding functions  in the Unix mathematics library, except that all functions accept  integer, real, and complex arguments, unless otherwise noted. The  sgn function is also supported, as in BASIC.  

    * abs: The abs function returns the absolute  value of its argument. The returned value is of the same type  as the argument. For complex arguments, abs(x) is defined as  the length of x in the complex plane [i.e., sqrt(real(x)**2  + imag(x)**2) ].

    * acos: The acos function returns the arc cosine  (inverse cosine) of its argument. acos returns its argument in  radians.

    * arg: The arg function returns the phase of a  complex number, in radians.

    * asin: The asin function returns the arc sin  (inverse sin) of its argument. asin returns its argument in radians.

    * tan: The atan function returns the arc tangent  (inverse tangent) of its argument. atan returns its argument in  radians.

    * ceil: The ceil function returns the smallest  integer that is not less than its argument. For complex numbers,  ceil returns the smallest integer not less than the real  part of  its argument.

    * cos: The cos function returns the cosine of  its argument. cos expects its argument to be in radians.

    * cosh: The cosh function returns the  hyperbolic cosine of its argument.

    * exp: The exp function returns the exponential  function of its argument (e raised to the power of its argument).

    * floorThe floor function returns the largest  integer not greater than its argument. For complex numbers, floor  returns the largest integer not greater than the real part of its  argument.

    * imag: The imag function returns the imaginary  part of its argument as a real number.

    * int: The int function returns the integer part  of its argument, truncated toward zero.

    * log: The log function returns the natural  logarithm (base e) of its argument.

    * log10: The log10 function returns the logarithm  (base 10) of its argument.

    * real The real function returns the real part of  its argument.

    * sgn: The sgn function returns 1 if its argument  is positive, -1 if its argument is negative, and 0 if its argument is 0. If the argument is a complex value, the imaginary component is  ignored.

    * sin: The sin function returns the sine of its  argument. sin expects its argument to be in radians.

    * sinh: The sinh function returns the hyperbolic  sine of its argument. 

    * sqrt: The sqrt function returns the square root  of its argument.

    * tan: The tan function returns the tangent of  its argument. tan expects its argument to be in radians.

    * tanh: The tanh function returns the hyperbolic  tangent of its argument.

Operators

    The operators in GNUPlot are the same as the corresponding operators  in the C programming language, except that all operators accept  integer, real, and complex arguments, unless otherwise noted.  The ** operator (exponentiation) is supported, as in FORTRAN.  Parentheses may be used to change order of evaluation.

    The following is a list of all the binary operators and their usages: 

* a*b multiplication
/ a/b division
% a%b mudolu
+ a+b addition
- a-b substraction
= = a = = b equality
! = a! = b inequality
& & a & & b logical AND
| | a | | b logical OR

 

    where the factorial operator returns a real  number to allow a greater range.  

Help

    The help command displays on-line help. To specify information  on a particular topic use the syntax: help topic.  If pic is not specified, a short message is printed  about GNUPlot. After help for the requested topic is given,  help for a subtopic may be requested by typing its name, extending  the help request. After that subtopic has been printed, the request  may be extended again, or simply pressing return goes back one level  to the previous topic. Eventually, the GNUPlot command line will  return.

Load

    The load command executes each line of the specified input file as if it had been typed in interactively. Files created by the  save command can later be loaded. Any text file containing valid  commands can be created and then executed by the load command.  Files being loaded may themselves contain load commands. See  comment for information about comments in commands. The load  command must be the last command on the line. The syntax of  the command is load "inputfile". The name of the input file must be enclosed in (either single or double) quotes.  The load command is performed implicitly on any file names  given as arguments to GNUPlot. These are loaded in the order specified, and then GNUPlot exits.

    This informations about two dimensional plotting seem to be  sufficient for a beginner to GNUPlot. The three dimensional plotting command is plotand it is quite similar to the  plot command. We suffice with this information here.  However to motivate the student we give some demo programs and  their display outputs. The students are responsible to investigate  the programs and their outputs in a comparative approach and  to get information about further features of GNUPlot. The   documents for GNUPlot are accessible via web site of \.IT\"U  Informatics Institute. 

Program 1

    plot [-20:20] sin(x), atan(x), cos(atan(x)

7.jpg (9352 bytes)

Program 2

    plot [-20:20] sin(30*x)*atan(x)

8.jpg (10779 bytes)

Program 3

9.jpg (14756 bytes)

Program 4

10.jpg (12629 bytes)

Program 5

11.jpg (14294 bytes)

Program 6

12.jpg (21065 bytes)

Program 7

13.jpg (15285 bytes)

Program 8

14.jpg (14757 bytes)

Program 9

15.jpg (14529 bytes)

Program 10

16.jpg (19534 bytes)

Program 11

17.jpg (17580 bytes)

Program 12

18.jpg (16658 bytes)

Program 13

19.jpg (20248 bytes)

Program 14

20.jpg (18358 bytes)

Program 15

21.jpg (18732 bytes)

Program 16

22.jpg (16110 bytes)

Program 17

23_1.jpg (17485 bytes)

23_2.jpg (32821 bytes)

Program 18

24_1.jpg (23576 bytes)

24_2.jpg (28180 bytes)        

Program 19

25.jpg (20362 bytes)

Program 20

26.jpg (17735 bytes)

Program 21

27.jpg (17463 bytes)

Program 22

28.jpg (24284 bytes)

Program 23

29.jpg (22926 bytes)

Program 24

30.jpg (23083 bytes)

Program 25

31.jpg (16792 bytes)

Program 26

32.jpg (29258 bytes)

Program 27

33.jpg (32373 bytes)

Program 28

34.jpg (25093 bytes)

Program 29

35.jpg (20404 bytes)

Program 30

36.jpg (23070 bytes)

Program 31

37.jpg (27626 bytes)

Program 32

38_1.jpg (22500 bytes)

38_2.jpg (8960 bytes)

Program 33

39.jpg (23669 bytes)

Program 34

40_1.jpg (14652 bytes)

40_2.jpg (9979 bytes)

Program 35

41_1.jpg (16142 bytes)

41_2.jpg (9148 bytes)

Program 36

42_1.jpg (23060 bytes)

42_2.jpg (7808 bytes)

Program 37

43_1.jpg (11498 bytes)

43_2.jpg (12742 bytes)

Program 38

44.jpg (28211 bytes)

Program 39

45_1.jpg (17486 bytes)

45_2.jpg (8885 bytes)

Program 40

46_1.jpg (17995 bytes)

46_2.jpg (8221 bytes)

Program 41

47_1.jpg (19604 bytes)

47_2.jpg (9688 bytes)

   The sample programs presented here were actually tested.  However they may contain some extra commands which are  not needed to produce the relevant display. The students  can try to find those type of extra commands by manually  testing the programs with and without the commands which ay seem to be unnecessary. This inconvenience do not  appear so frequently but the author could not have the  chance of the cleaning these kind of structures. 

    The sample programs are mostly taken from the demo  directory of the GNUPlot documentation which comes  with the program under SuSE Linux. It is freely accessible   under GNU Public Licence so it Copyright Rules of this  type is valid also here.