User Manual Gnuplot
User Manual:
Open the PDF directly: View PDF
.
Page Count: 13
| Download | |
| Open PDF In Browser | View PDF |
Using Gnuplot on Linux Platform Wadhwani Electronics Laboratory, IITB. July 10, 2014 Contents 1 Installation 1.1 Using Terminal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.2 Using Synaptic Package Manager . . . . . . . . . . . . . . . . . . . . . . . 2 Using Gnuplot on linux Platform 2.1 Points to note . . . . . . . . . . . . . . . . . . 2.2 Plotting data-points from a file . . . . . . . . 2.2.1 Viewing Plots . . . . . . . . . . . . . . 2.2.2 Saving the Output file . . . . . . . . . 2.2.3 Customizing plot area . . . . . . . . . 2.2.4 Plotting multiple curves from data file 2.3 Curve fitting . . . . . . . . . . . . . . . . . . . 2.4 Plotting direct functions . . . . . . . . . . . . 2.4.1 Basics . . . . . . . . . . . . . . . . . . 2.4.2 Multiplots . . . . . . . . . . . . . . . . A Supported functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 2 2 3 3 3 3 4 5 6 7 9 9 10 12 1 Chapter 1 Installation 1.1 Using Terminal • Open Terminal. • Type "sudo apt-get install gnuplot" and press Enter. • Type "sudo apt-get install gnuplot-x11" and press Enter. 1.2 Using Synaptic Package Manager • Open Synaptic Package Manager and type gnuplot in Quick Filter search. • Right click on gnuplot package and click Mark for Installation. • Similarly select gnuplot-x11 and click Mark for Installation. • Now install packages by clicking Apply. 2 Chapter 2 Using Gnuplot on linux Platform 2.1 Points to note • GnuPlot ignores lines starting with # (Symbol for comments). • “**” mean exponent. e.g. “3**4” means 34 . • Any file name in command must be mentioned in double quotes. e.g. “file.txt”. • • 2.2 2.2.1 Plotting data-points from a file Viewing Plots Many Computing simulators give output in data files (with .dat extension). Each line in data file specifies one data point which has one independent variable and one or more dependent variable which are seperated by [space] or [Tab]. Here we will plot data points using Gnuplot. • Open your favorite text editor (e.g. gedit), copy follwing text and save it with .dat extension (e.g. test.dat) to the prefered directory. #this file contais data points having two dependent variables. #X ramdom Xsquare 0 0.1 0 1 1.1 1 2 2.1 4 3 3.2 9 4 4.3 16 5 5.5 25 • Open terminal and go to the directory (where you saved your file) using cd command. 3 Wadhwani Electronics Laboratory (WEL) IIT Bombay • Type gnuplot and press Enter. • When Gnuplot is launched in terminal, the last line should be “Terminal type set to ‘wxt’ ” if not, gnuplot-x11 package is not installed properly and hence it will not generate plots. • Type plot "test.dat" and Enter. Plot window should pop-out. • By default, it will plot first two columns, first on X-axis and second on Y-axis. • Pressing ‘l’ on keyboard will change Y-axis of plot to log-scale. • Similarely ‘a’ for autoscale, ‘r’ for ruler, ‘g’ for grid, ‘b’ for border. 2.2.2 Saving the Output file • Type set terminal png and Enter To get output in png format. It can also be set to gif, jpeg and many more file types For more information, type set terminal and press Enter. • Type set output "out.png" and Enter Output will be saved as out.png in the same directory after plotting. • Type replot and Enter You will observe that ranges for x & y axes and legend are automatially generated. For any update or modification in plot, only replot command is needed. Go to the directory and see output (see Figure 2.1 for reference). Figure 2.1: Plot of 1st dependent variable Page 4 Wadhwani Electronics Laboratory (WEL) 2.2.3 IIT Bombay Customizing plot area Plot area can be modified using following commands and entering replot afterwards. Table 2.1: Modifying plot area Command Purpose To To To To To To To To To add x-label add y-label add Title set sample points set x or y range set grid set x tics set log x scale set log y scale set xlabel "Independent-variable" set ylabel "Dependent-variable" set title "plot-title" set samples n set xrange [x1 :x2 ], set yrange [y1 :y2 ] set grid set xtics, , set logscale unset logscale; set logscale y • In Figure 2.1, crosspoints are used to denote the datapoints. We can Customize it to impulse plot or line plot or line+point plot etc. also the type of dots and its size can be adjusted. plot "test.dat" with lines Replace “lines” in above command with “lines linestyle 2 linewidth 3”, “impulse”, “linespoints”, “points pointtype 7 pointsize 2” and play around for more insight. • To modify position of legend, Try follwing commands. set key left box / set key right nobox / set key reverse Left outside Following figure shows same plot with modified labels and legend. Figure 2.2: Plot after modification Page 5 Wadhwani Electronics Laboratory (WEL) 2.2.4 IIT Bombay Plotting multiple curves from data file • Using simple plot command as used above, we were not able to plot multiple dependent variables in data file. For that we will use more specific commands as given below. plot "test.dat" using 1:2 title ‘Random’ with linespoints pointtype 3, "test.dat" using 1:3 title ‘Xsquared’ with line Term “1:2” means x=1st column and y=2nd column Term “1:3” means y=1st column and y=3nd column. • You may have multiple data files. In this case, you may make use a command like: plot "test1.dat" using 1:2 title ‘Test1-data’ with linespoints pointtype 3,"test2.dat" using 1:3 title ‘Test2-data’ with line • Play with some modifications for proper understanding. See Multiple curves in following figure which shows two dependent variables from data file. Figure 2.3: Plot of Multiple dependent variable Page 6 Wadhwani Electronics Laboratory (WEL) 2.3 IIT Bombay Curve fitting • Curve fitting in data is necessory tool for any plotting software. Gnuplot supports linear as well as nonlinear curve fits. • Our first column data can be fitted into line f (x) = a ∗ x + b. • To fit it in equation of line, follow the commands one by one: plot "test.dat" using 1:2 title ‘Random’ f(x)=m*x+n fit f(x) "test.dat" using 1:2 via m,n replot f(x) • For fitting polynomial in second data. plot "test.dat" using 1:3 title ‘series-2’ g(x)=a*x**b fit g(x) "test.dat" using 1:3 via a,b replot g(x) • After successful fitting, it will show values of m & n or a & b in the terminal. • If it finds any singularity, then it will give error as “-nan”(it means Not A Number). To resolve it, give initial guess of values of m and n in terminal. e.g. m=1,n=1 . Or add some small value in f(x). e.g. f(x)=m*x+n+1e-8. If Problem continues, Find other ways (Google :)). • All graphs can be plotted simultaniously using following command :—> plot "test.dat" using 1:2 title ‘series-1’, f(x) title ‘Fit to series-1’, "test.dat" using 1:3 title ‘series-2’, g(x) title ‘Fit to series-2’ • Gnuplot support C-language syntax along with all mathematical formats. Variable can be displayed on graph using sprint command as shown below. set label set label set label set label replot 1 2 3 4 sprintf("m sprintf("n sprintf("a sprintf("b = = = = %3.4f",m) %3.4f",n) %3.4f",a) %3.4f",b) at at at at 1,15 1,13 1,11 1,9 —> last two numbers seperated by comma shows position of label on graph. • To save it —> set terminal png set output "fitted-out.png" replot Page 7 Wadhwani Electronics Laboratory (WEL) • Figure 2.5 shows the obtained plot. Figure 2.4: Plot of Multiple dependent variable Page 8 IIT Bombay Wadhwani Electronics Laboratory (WEL) 2.4 2.4.1 IIT Bombay Plotting direct functions Basics • plot is used for 2D graphs and splot is used for 3D graphs. • All standard mathematical expressions are valid for Gnuplot. See Examples. set samples 500 set xrange [-20:20] plot sin(x)/x • output will look like :—> Figure 2.5: Sinc Functin • For Supported functions see Appendix. • commands like following are also acceptable. plot [-pi/2:pi] cos(x),-(sin(x) > sin(x+1) ? Page 9 sin(x) : sin(x+1)) Wadhwani Electronics Laboratory (WEL) 2.4.2 IIT Bombay Multiplots Gnuplot can plot more than one figure in same figure (like subplot in Matlab). Try following commands in terminal one after another: set set set set set multiplot size 1,0.5 origin 0.0,0.5; plot sin(x)**2/x origin 0.0,0.0; plot real(tan(x)/atan(x)) grid unset multiplot • In second command, (1,0.5) means row is splitted in half. (2 subplots) (0.5,0.5) means row and column are splitted in half. (4 subplots) (0.25,0.25) means row and column are spitted in four parts. (16 subplots) • Obtained output is —> Figure 2.6: Multiplots Page 10 Bibliography The Gnuplot examples —> http://gnuplot.sourceforge.net/demo/ The Gnuplot Manual —> http://www.gnuplot.info/docs_4.6/gnuplot.pdf The Gnuplot FAQ —> http://www.gnuplot.info/faq/ 11 Appendix A Supported functions Table A.1: Supported Functions Functions Return abs(x) acos(x) asin(x) atan(x) cos(x) cosh(x) erf(x) exp(x) inverf(x) invnorm(x) log(x) log10(x) norm(x) rand(x) sgn(x) sin(x) sinh(x) sqrt(x) tan(x) tanh(x) absolute value of x, |x| arc-cosine of x arc-sine of x arc-tangent of x cosine of x, x is in radians. hyperbolic cosine of x, x is in radians error function of x exponential function of x, base e inverse error function of x inverse normal distribution of x log of x, base e log of x, base 10 normal Gaussian distribution function pseudo-random number generator 1 if x > 0, -1 if x < 0, 0 if x=0 sine of x, x is in radians hyperbolic sine of x, x is in radians the square root of x tangent of x, x is in radians hyperbolic tangent of x, x is in radians NOTE: Bessel, gamma, ibeta, igamma, and lgamma functions are also supported. Many functions can take complex arguments. Binary and unary operators are also supported. 12
Source Exif Data:
File Type : PDF File Type Extension : pdf MIME Type : application/pdf PDF Version : 1.5 Linearized : No Page Count : 13 Page Mode : UseOutlines Author : Title : Subject : Creator : LaTeX with hyperref package Producer : pdfTeX-1.40.13 Create Date : 2014:07:09 14:26:10+05:30 Modify Date : 2014:07:09 14:26:10+05:30 Trapped : False PTEX Fullbanner : This is pdfTeX, Version 3.1415926-2.4-1.40.13 (TeX Live 2012/Debian) kpathsea version 6.1.0EXIF Metadata provided by EXIF.tools