BIL101, Introduction to Computers  and  Information Systems Chapter 12

25
BIL101, Introduction to Computers BIL101, Introduction to Computers and and Information Systems Information Systems Chapter 12 Chapter 12 A Portable Scientific Visualization Program: A Portable Scientific Visualization Program: GnuPlot GnuPlot Prepared by Prepared by Metin Demiralp Metin Demiralp Istanbul Technical University, Informatics Istanbul Technical University, Informatics Institute, Institute, Maslak { 80626, Istanbul, Türkiye) Maslak { 80626, Istanbul, Türkiye) Version 0. 60 Version 0. 60

description

BIL101, Introduction to Computers  and  Information Systems Chapter 12. A Portable Scientific Visualization Program: GnuPlot Prepared by Metin Demiralp Istanbul Technical University, Informatics Institute, Maslak { 80626, Istanbul, Türkiye) Version 0. 60. - PowerPoint PPT Presentation

Transcript of BIL101, Introduction to Computers  and  Information Systems Chapter 12

Page 1: BIL101, Introduction to Computers  and   Information Systems  Chapter 12

BIL101, Introduction to Computers  and  BIL101, Introduction to Computers  and  Information Systems Information Systems

Chapter 12Chapter 12

A Portable Scientific Visualization Program: A Portable Scientific Visualization Program:

GnuPlotGnuPlot         

Prepared byPrepared byMetin DemiralpMetin Demiralp

Istanbul Technical University, Informatics Institute,Istanbul Technical University, Informatics Institute,Maslak { 80626, Istanbul, Türkiye)Maslak { 80626, Istanbul, Türkiye)

Version 0. 60Version 0. 60

Page 2: BIL101, Introduction to Computers  and   Information Systems  Chapter 12

A Portable Scientific Visualization A Portable Scientific Visualization Program: Program: GnuPlotGnuPlot

The subject is mostly aimed at the The subject is mostly aimed at the scientific visualization. Since GNUPlot is scientific visualization. Since GNUPlot is one of the most powerful and easily one of the most powerful and easily accessible world wide used free scientific accessible world wide used free scientific visualization program we concern with it. visualization program we concern with it. GNUPlot are come with almost all GNUPlot are come with almost all distributions for also Windows and some distributions for also Windows and some other operating systems.other operating systems.

GNUPlot is an interactive function plotting GNUPlot is an interactive function plotting program. It has interactive or batch program. It has interactive or batch modes.modes.

Page 3: BIL101, Introduction to Computers  and   Information Systems  Chapter 12

To invoke interactive mode it is To invoke interactive mode it is sufficient to give the sufficient to give the GnuPlotGnuPlot command command at the command prompt of linux. When at the command prompt of linux. When this is done a prompt appears and this is done a prompt appears and prompts the user to give gnuplot prompts the user to give gnuplot commands. The commands of GnuPlot commands. The commands of GnuPlot may need parameters and arguments may need parameters and arguments depending on what you want them to depending on what you want them to do.do.

GnuPlotGnuPlot syntax is case sensitive syntax is case sensitive (commands and function names written (commands and function names written in lowercase are not the same as those in lowercase are not the same as those written in capital letters.) written in capital letters.)

Page 4: BIL101, Introduction to Computers  and   Information Systems  Chapter 12

You can abbrivate the command names You can abbrivate the command names as long as the abbreviation is not as long as the abbreviation is not ambiguous. Any number of commands ambiguous. Any number of commands may appear on a line, separated by may appear on a line, separated by semicolons (;). Strings are indicated with semicolons (;). Strings are indicated with quotes. They may be either single or quotes. They may be either single or double quotation marks.double quotation marks.

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

Page 5: BIL101, Introduction to Computers  and   Information Systems  Chapter 12

In this display output, the display range is selected In this display output, the display range is selected between -10 and 10 for the x axis and between -1 and 1 between -10 and 10 for the x axis and between -1 and 1 for the y axis. These are default values for the plotting for the y axis. These are default values for the plotting range of the sine function. range of the sine function.

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.

Page 6: BIL101, Introduction to Computers  and   Information Systems  Chapter 12

The specification of the horizontal range needs two The specification of the horizontal range needs two numerical values for the beginning and the end numerical values for the beginning and the end points.points.

plot [-5:5] sin (x)

command selects the horizontal plotting range between -5 and 5.

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,

Page 7: BIL101, Introduction to Computers  and   Information Systems  Chapter 12

They must be separated by colon and They must be separated by colon and enclosed by the brackets. One or both of the enclosed by the brackets. One or both of the numerical values can be skipped. In that numerical values can be skipped. In that case either left or right side of the colon will case either left or right side of the colon will include nothing. If both of the numerical include nothing. If both of the numerical values is not given then inside of the values is not given then inside of the brackets contains nothing. brackets contains nothing.

The display output of this command, the ticks The display output of this command, the ticks on the horizontal axis are positioned by on the horizontal axis are positioned by GnuPlotGnuPlot which has used an interval whose which has used an interval whose length equals 2 between ticks. This can be length equals 2 between ticks. This can be changed. changed.

Page 8: BIL101, Introduction to Computers  and   Information Systems  Chapter 12

For this we can modify the last example For this we can modify the last example as follows: as follows: set xtics 1 set xtics 1 plot [-5:5] sin (X)plot [-5:5] sin (X) where the interval between two where the interval between two consecutive ticks is set to 1. consecutive ticks is set to 1.

Page 9: BIL101, Introduction to Computers  and   Information Systems  Chapter 12

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

Page 10: BIL101, Introduction to Computers  and   Information Systems  Chapter 12

This means the nonexistence of the ticks This means the nonexistence of the ticks on the horizontal axis.on the horizontal axis.

The range for the vertical coordinates can The range for the vertical coordinates can also be set by the user. For this purpose also be set by the user. For this purpose we can use an additional pair of brackets. we can use an additional pair of brackets. For example, the command For example, the command plot [-5:5] [-2:2] Sin(x)plot [-5:5] [-2:2] Sin(x)

Page 11: BIL101, Introduction to Computers  and   Information Systems  Chapter 12

On the other hand the vertical ticks can be On the other hand the vertical ticks can be controlled by using the controlled by using the set yticsset ytics command as command as done in the following sample command.done in the following sample command. set set ytics 1 ytics 1 plot [-5:5] [-plot [-5:5] [-2:2] Sin (x) 2:2] Sin (x) It is possible to plot It is possible to plot more than one function at a single attempt. more than one function at a single attempt. For this purpose the functions to be plotted For this purpose the functions to be plotted must be given in comma separated form like must be given in comma separated form like the following command: the following command: set set xticks 1; set ytics 1 xticks 1; set ytics 1 plot [-5:5] [-plot [-5:5] [-2:2] Sin (x), Cos (x), tan (x)2:2] Sin (x), Cos (x), tan (x)

Page 12: BIL101, Introduction to Computers  and   Information Systems  Chapter 12

This plots the trigonometric functions, sine, This plots the trigonometric functions, sine, cosine, and tangent. cosine, and tangent. We can mention about some other features We can mention about some other features of GNUPlot. of GNUPlot. CommentComment

In GNUPlot comments are created by using In GNUPlot comments are created by using the character #. This symbol can be the character #. This symbol can be positioned at the beginning of or somewhere positioned at the beginning of or somewhere else in a line. GNUPlot ignores the remaining else in a line. GNUPlot ignores the remaining portion of the line after this character.portion of the line after this character.

Page 13: BIL101, Introduction to Computers  and   Information Systems  Chapter 12

The effect of this character vanish The effect of this character vanish between quotation marks, inside between quotation marks, inside numbers and inside command numbers and inside command substitutions. As a matter of fact it is substitutions. As a matter of fact it is alive anywhere it makes sense to work. alive anywhere it makes sense to work.

Exit Exit

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

Page 14: BIL101, Introduction to Computers  and   Information Systems  Chapter 12

FunctionsFunctions

The functions in GNUPlot are the same The functions in GNUPlot are the same as the corresponding functions in the as the corresponding functions in the Unix mathematics library, except Unix mathematics library, except that all functions accept integer, real, that all functions accept integer, real, and complex arguments, unless and complex arguments, unless otherwise noted. The sgn function is otherwise noted. The sgn function is also supported, as in BASIC. also supported, as in BASIC. * abs:* abs: The abs function The abs function returns the absolute value of its returns the absolute value of its argument. The returned value is of argument. The returned value is of the same type as the argument. the same type as the argument.

Page 15: BIL101, Introduction to Computers  and   Information Systems  Chapter 12

• acos: acos: The acos function returns the arc The acos function returns the arc cosine (inverse cosine) of its cosine (inverse cosine) of its argument. acos returns its argument. acos returns its argument in radians. argument in radians. * arg:* arg: The arg function returns The arg function returns the phase of a complex number, in the phase of a complex number, in radians. radians.

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

Page 16: BIL101, Introduction to Computers  and   Information Systems  Chapter 12

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

• ceil:ceil: The ceil function The ceil function returns the returns the smallest integer that is not less than its smallest integer that is not less than its argument. argument.

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

Page 17: BIL101, Introduction to Computers  and   Information Systems  Chapter 12

* exp:* exp: The exp function returns the exponential The exp function returns the exponential function of its argument (e raised to the function of its argument (e raised to the power of its argument). power of its argument). * floor:* floor: The floor function returns the The floor function returns the largest integer not greater than its argument. largest integer not greater than its argument.

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

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

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

Page 18: BIL101, Introduction to Computers  and   Information Systems  Chapter 12

* log10: * log10: The log10 function returns the The log10 function returns the logarithm (base 10) of its argument. logarithm (base 10) of its argument. * real:* real: The real function The real function returns the real part of its argument. returns the real part of its argument. * sgn:* sgn: The sgn function returns 1 if its The sgn function returns 1 if its argument is positive, -1 if its argument is positive, -1 if its argument is negative, and 0 if its argument is negative, and 0 if its argument is 0. If the argument is a argument is 0. If the argument is a complex value, the imaginary complex value, the imaginary component is ignored. component is ignored.

Page 19: BIL101, Introduction to Computers  and   Information Systems  Chapter 12

sin:sin: The sin function returns the sine of The sin function returns the sine of its argument. sin expects its argument its argument. sin expects its argument to be in radians. to be in radians. * sinh: * sinh: The sinh function The sinh function returns the hyperbolic sine of its returns the hyperbolic sine of its argument. argument. * * sqrt:sqrt: The sqrt function returns the The sqrt function returns the square root of its argument. square root of its argument. * tan:* tan: The tan function The tan function returns the tangent of its argument. tan returns the tangent of its argument. tan expects its argument to be in radians. expects its argument to be in radians.

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

Page 20: BIL101, Introduction to Computers  and   Information Systems  Chapter 12

Operators Operators

The operators in GNUPlot are the same The operators in GNUPlot are the same as the corresponding operators in the as the corresponding operators in the C programming language, except that C programming language, except that all operators accept integer, real, and all operators accept integer, real, and complex arguments, unless otherwise complex arguments, unless otherwise noted. noted.

The ** operator (exponentiation) is The ** operator (exponentiation) is supported, as in FORTRAN. supported, as in FORTRAN. Parantheses may be used to change Parantheses may be used to change order of evaluation.order of evaluation.

Page 21: BIL101, Introduction to Computers  and   Information Systems  Chapter 12

The following is a list of all the binary The following is a list of all the binary operators and their usages: operators and their usages: Symbol Example Symbol Example ExplanationExplanation ** a**b ** a**b exponentiation exponentiation

* a*b multiplication * a*b multiplication / a/b division / a/b division % a%b modulo % a%b modulo + a+b addition + a+b addition - a-b subtraction - a-b subtraction == a==b equality == a==b equality !=!= a!=b inequality a!=b inequality && a&&b logical AND && a&&b logical AND ll allb logical ORll allb logical OR where the modulo, logical AND and logical where the modulo, logical AND and logical

OR requires integer OR requires integer arguments. arguments.

Page 22: BIL101, Introduction to Computers  and   Information Systems  Chapter 12

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

Symbol Example ExplanationSymbol Example Explanation

- -a unary minus - -a unary minus

! !a logical negation! !a logical negation

! a! factorial ! a! factorial

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

Page 23: BIL101, Introduction to Computers  and   Information Systems  Chapter 12

Help Help The help command displays on-line help. The help command displays on-line help.

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

Page 24: BIL101, Introduction to Computers  and   Information Systems  Chapter 12

LoadLoad The The loadload command executes each line of command executes each line of

the specified input file as if it had been the specified input file as if it had been typed in interactively. Files creates by the typed in interactively. Files creates by the save command can later be loaded. Any save command can later be loaded. Any text file containing valid commands can text file containing valid commands can be created and then executed by the load be created and then executed by the load command. Files being loaded may command. Files being loaded may themselves contain load commands. The themselves contain load commands. The load command must be the last command load command must be the last command on the line. The syntax of the command is on the line. The syntax of the command is load “inputfile”load “inputfile”. The name of the inputfile . The name of the inputfile must be enclosed in (either single or must be enclosed in (either single or double) quotes. double) quotes.

Page 25: BIL101, Introduction to Computers  and   Information Systems  Chapter 12

The load command is performed The load command is performed implicitly on any file names given as implicitly on any file names given as arguments to GNUPlot.arguments to GNUPlot.

These are loaded in the order specified These are loaded in the order specified and then GNUPlot exits.and then GNUPlot exits.

This informations about two This informations about two dimensional plotting seem to be dimensional plotting seem to be sufficient for a beginner to GNUPlot. sufficient for a beginner to GNUPlot. The three dimensional plotting The three dimensional plotting command is command is splotsplot and it is quite and it is quite similar to the similar to the plotplot command. command.