39
1 A Portable Scientific A Portable Scientific Visualization Program: Visualization Program: GnuPlot GnuPlot Ass Ass t t . Prof. Emin Korkut . Prof. Emin Korkut

1 A Portable Scientific Visualization Program: GnuPlot Asst. Prof. Emin Korkut

  • View
    225

  • Download
    4

Embed Size (px)

Citation preview

Page 1: 1 A Portable Scientific Visualization Program: GnuPlot Asst. Prof. Emin Korkut

1

A Portable Scientific A Portable Scientific Visualization Program: GnuPlotVisualization Program: GnuPlot

AssAsstt. Prof. Emin Korkut. Prof. Emin Korkut

Page 2: 1 A Portable Scientific Visualization Program: GnuPlot Asst. Prof. Emin Korkut

2

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. 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.

Page 3: 1 A Portable Scientific Visualization Program: GnuPlot Asst. Prof. Emin Korkut

3

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

gnuplot>plot sin(x)

-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

-10 -5 0 5 10

sin(x)

Page 4: 1 A Portable Scientific Visualization Program: GnuPlot Asst. Prof. Emin Korkut

4

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

Page 5: 1 A Portable Scientific Visualization Program: GnuPlot Asst. Prof. Emin Korkut

5

-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

-4 -2 0 2 4

sin(x)

Page 6: 1 A Portable Scientific Visualization Program: GnuPlot Asst. Prof. Emin Korkut

6

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.

Page 7: 1 A Portable Scientific Visualization Program: GnuPlot Asst. Prof. Emin Korkut

7

-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

-5 -4 -3 -2 -1 0 1 2 3 4 5

sin(x)

Page 8: 1 A Portable Scientific Visualization Program: GnuPlot Asst. Prof. Emin Korkut

8

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.

-2

-1.5

-1

-0.5

0

0.5

1

1.5

2

-5 -4 -3 -2 -1 0 1 2 3 4 5

sin(x)

Page 9: 1 A Portable Scientific Visualization Program: GnuPlot Asst. Prof. Emin Korkut

9

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)

-2

-1

0

1

2

-5 -4 -3 -2 -1 0 1 2 3 4 5

sin(x)

Page 10: 1 A Portable Scientific Visualization Program: GnuPlot Asst. Prof. Emin Korkut

10

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.

Page 11: 1 A Portable Scientific Visualization Program: GnuPlot Asst. Prof. Emin Korkut

11

-2

-1

0

1

2

-5 -4 -3 -2 -1 0 1 2 3 4 5

sin(x)cos(x)tan(x)

Page 12: 1 A Portable Scientific Visualization Program: GnuPlot Asst. Prof. Emin Korkut

12

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 quit and the END-OF-FILE character exit GNUPlot. All these commands clear the output device before exiting.

Page 13: 1 A Portable Scientific Visualization Program: GnuPlot Asst. Prof. Emin Korkut

13

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.

Page 14: 1 A Portable Scientific Visualization Program: GnuPlot Asst. Prof. Emin Korkut

14

* 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).

Page 15: 1 A Portable Scientific Visualization Program: GnuPlot Asst. Prof. Emin Korkut

15

* floor: The 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.

Page 16: 1 A Portable Scientific Visualization Program: GnuPlot Asst. Prof. Emin Korkut

16

* 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.

Page 17: 1 A Portable Scientific Visualization Program: GnuPlot Asst. Prof. Emin Korkut

17

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:

Page 18: 1 A Portable Scientific Visualization Program: GnuPlot Asst. Prof. Emin Korkut

18

* a*b multiplication

/ a/b division

% a%b modulo

+ a+b addition

- a-b subtraction

= = 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.

Page 19: 1 A Portable Scientific Visualization Program: GnuPlot Asst. Prof. Emin Korkut

19

Help

The help command displays on-line help.

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. 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.

Page 20: 1 A Portable Scientific Visualization Program: GnuPlot Asst. Prof. Emin Korkut

20

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

-2

-1

0

1

2

-20 -15 -10 -5 0 5 10 15 20

sin(x)atan(x)

cos(atan(x))

Page 21: 1 A Portable Scientific Visualization Program: GnuPlot Asst. Prof. Emin Korkut

21

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

-2

-1

0

1

2

-20 -15 -10 -5 0 5 10 15 20

sin(30*x)*atan(x)

Page 22: 1 A Portable Scientific Visualization Program: GnuPlot Asst. Prof. Emin Korkut

22

set parametricset dummy tset autoscaleset title “t, sin(t)/t or sin(x)/x”plot t, sin(t)/t

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

-6 -4 -2 0 2 4 6

t, sin(t)/t or sin(x)/x

t, sin(t)/t

Page 23: 1 A Portable Scientific Visualization Program: GnuPlot Asst. Prof. Emin Korkut

23

set parametricset dummy tset autoscaleset title “”plot sin(t)/t, t

-6

-4

-2

0

2

4

6

-0.4 -0.2 0 0.2 0.4 0.6 0.8 1

sin(t)/t, t

Page 24: 1 A Portable Scientific Visualization Program: GnuPlot Asst. Prof. Emin Korkut

24

set parametricset dummy tset autoscaleset samples 160set title “”plot sin(t), cos(t)

-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1

sin(t), cos(t)

Page 25: 1 A Portable Scientific Visualization Program: GnuPlot Asst. Prof. Emin Korkut

25

set parametricset dummy tset autoscaleset xrange [-3:3]set yrange [-3:3]set title “Parametric conic sections”plot –t, t, cos(t), cos(2*t), 2*cos(t), sin(t), -cosh(t), sinh(t)set title “”

-3

-2

-1

0

1

2

3

-3 -2 -1 0 1 2 3

Parametric conic sections

-t, tcos(t), cos(2*t)2*cos(t), sin(t)

-cosh(t), sinh(t)

Page 26: 1 A Portable Scientific Visualization Program: GnuPlot Asst. Prof. Emin Korkut

26

set parametricset dummy tset autoscaleset xrange [-5:5]set yrange [-5:5]plot tan(t), t, t, tan(t)

-4

-2

0

2

4

-4 -2 0 2 4

tan(t), tt, tan(t)

Page 27: 1 A Portable Scientific Visualization Program: GnuPlot Asst. Prof. Emin Korkut

27

set parametricset dummy tset autoscaleset trange [0.00001:3]plot t, log(t), -t, log(t), sin(t), t**2, -sin(t), t**2

-15

-10

-5

0

5

10

-3 -2 -1 0 1 2 3

t, log(t)-t, log(t)

sin(t), t**2-sin(t), t**2

Page 28: 1 A Portable Scientific Visualization Program: GnuPlot Asst. Prof. Emin Korkut

28

set parametricset dummy tset autoscaleset yrange [-1.5:1.5]set trange [0.0001:10*pi]plot sin(t)/t, cos(t)/t

-1.5

-1

-0.5

0

0.5

1

1.5

-0.4 -0.2 0 0.2 0.4 0.6 0.8 1

sin(t)/t, cos(t)/t

Page 29: 1 A Portable Scientific Visualization Program: GnuPlot Asst. Prof. Emin Korkut

29

set noborderset clipset polarset autoscaleset xtics axis nomirrorset ytics axis nomirrorset zeroaxisset trange [0:2*pi]set title “Three circles (with aspect ratio distortion)”plot .5,1,1.5

1.5

1

0.5

0

0.5

1

1.5

1.5 1 0.5 0 0.5 1 1.5

Three circles (with aspect ratio distortion)

.51

1.5

Page 30: 1 A Portable Scientific Visualization Program: GnuPlot Asst. Prof. Emin Korkut

30

set noborderset clip; set polarset xtics axis nomirror; set ytics axis nomirrorset samples 160; set zeroaxisset trange [0:2*pi]set title “”plot cos(2*t)

1

0.8

0.6

0.4

0.2

0

0.2

0.4

0.6

0.8

1

1 0.8 0.6 0.4 0.2 0 0.2 0.4 0.6 0.8 1

cos(2*t)

Page 31: 1 A Portable Scientific Visualization Program: GnuPlot Asst. Prof. Emin Korkut

31

set noborderset clip; set polarset xtics axis nomirror; set ytics axis nomirrorset samples 160; set zeroaxisset trange [0:2*pi]set title “”plot 2*sqrt(cos(t)), -2*sqrt(cos(t))

1.5

1

0.5

0

0.5

1

1.5

2 1.5 1 0.5 0 0.5 1 1.5 2

2*sqrt(cos(t))-2*sqrt(cos(t))

Page 32: 1 A Portable Scientific Visualization Program: GnuPlot Asst. Prof. Emin Korkut

32

set noborderset clip; set polarset xtics axis nomirror; set ytics axis nomirrorset samples 160; set zeroaxisset trange [0:2*pi]plot sin(4*t), cos(4*t)

1

0.8

0.6

0.4

0.2

0

0.2

0.4

0.6

0.8

1

1 0.8 0.6 0.4 0.2 0 0.2 0.4 0.6 0.8 1

sin(4*t)cos(4*t)

Page 33: 1 A Portable Scientific Visualization Program: GnuPlot Asst. Prof. Emin Korkut

33

set samples 21; set isosamples 11set xlabel “X axis” –3,-2; set ylabel “Y axis” 3,-2set zlabel “Z axis” -5; set title “3D gnuplot demo”set label 1 “This is the surface boundary” at –10,-5,150 centerset arrow 1 from –10,-5,120 to –10,0,0 noheadset arrow 2 from –10,-5,120 to 10,0,0 noheadset arrow 3 from –10,-5,120 to 0,10,0 noheadset arrow 4 from –10,-5,120 to 0,-10,0 noheadset xrange [-10:10]; set yrange [-10,10]splot x*y

3D gnuplot demo

This is the surface boundary

x*y

-10-5

05

10

X axis

-10

-5

0

5

10

Y axis

-100

-50

0

50

100

Z axis

Page 34: 1 A Portable Scientific Visualization Program: GnuPlot Asst. Prof. Emin Korkut

34

set samples 21; set isosamples 11set xrange [-10:10]; set yrange [-10,10]set gridsplot x**2+y**2, x**2-y**2

x**2+y**2x**2-y**2

-10-5

05

10 -10

-5

0

5

10

-100-50

050

100150200

Page 35: 1 A Portable Scientific Visualization Program: GnuPlot Asst. Prof. Emin Korkut

35

set samples 21; set isosamples 11set xrange [-10:10]; set yrange [-10,10]set ticslevel 0.0set title “3D gnuplot demo (ticslevel = 0.0)”splot (x**3+y**3)/10

3D gnuplot demo (ticslevel = 0.0)

(x**3+y**3)/10

-10-5

05

10 -10

-5

0

5

10

-200

-150

-100

-50

0

50

100

150

200

Page 36: 1 A Portable Scientific Visualization Program: GnuPlot Asst. Prof. Emin Korkut

36

set samples 21; set isosamples 11set xrange [-10:10]; set yrange [-10,10]set ticslevel 2.0set title “3D gnuplot demo (ticslevel = 2.0)”splot (x**3+y**3)/10

3D gnuplot demo (ticslevel = 2.0)

(x**3+y**3)/10

-10-5

05

10 -10

-5

0

5

10

-200-150-100-50050100150200

Page 37: 1 A Portable Scientific Visualization Program: GnuPlot Asst. Prof. Emin Korkut

37

set samples 21; set isosamples 11set xrange [-10:10]; set yrange [-10,10]set gridset ticslevel 0.5set title “3D gnuplot demo (ticslevel = 0.5)”splot (x**3+y**3)/10

3D gnuplot demo (ticslevel = 0.5)

(x**3+y**3)/10

-10-5

05

10 -10

-5

0

5

10

-200-150-100-50

050

100150200

Page 38: 1 A Portable Scientific Visualization Program: GnuPlot Asst. Prof. Emin Korkut

38

set samples 21; set isosamples 11set xrange [-10:10]; set yrange [-10,10]splot x*y with points

3D gnuplot demo (ticslevel = 0.5)

x*y

-10-5

05

10 -10

-5

0

5

10

-100

-50

0

50

100

Page 39: 1 A Portable Scientific Visualization Program: GnuPlot Asst. Prof. Emin Korkut

39

set xtics autofreqset ytics autofreqset xrange [-1:1]; set yrange [-1,1]set samples 51; set isosample 21set dummy u,v; set title “3D gnuplot demo”splot u*v / (u**2+v**2+0.1)

3D gnuplot demo

u*v / (u**2+v**2+0.1)

-1-0.5

00.5

1 -20

24

68

10

-0.5-0.4-0.3-0.2-0.1

00.10.20.30.40.5