CSE4251 The Unix Programming Environment Lecture 13 gnuplot
What is Gnuplot A portable command-line driven graphing utility can output most of the picture formats eps/pdf/tex: good for writing academic pictures now added html, javascript: display data on web Can use interactive command and script file script is good to automate the task!
Interactive plot launch gnuplot: $ gnuplot $ gnuplot gnuplot> gnuplot> plot sin(x)
set plot range set xrange [0:5] # define x range set yrange [0:10] # define y range $ gnuplot gnuplot> gnuplot> plot sin(x) gnuplot > set xrange [0:5] gnuplot > replot
plot from script Save plot commands in file, and run gnuplot: $ gnuplot mycmds.plt Output disappears quickly; use pause to see the result, e.g.: plot sin(x) pause -1 "Hit any key to continue“
commonly used commands help topic #get help about a topic replot #repeat last plot command clear #remove objects in the canvas set OPTION [VALUE] #specify plot options set xrange [0:5] #set the horizontal axis range set multiplot #several plots on the same figure show [all] # show (all) the settings plot #primary command for drawing 2 D figures. plot sin(x) plot ‘datafilename’ using 2:3 with linespoints linetype 1 pointtype 2 title ‘MyFigure’ plot ‘datafilename’ u 2:3 w lp lt 1 pt 2 t ‘MyFigure’ #same meaning as above splot #drawing 3D figures.
save plot to a file Define terminal type and file name set terminal png #set terminal to png type #allow creating png format picture set output ‘output.png’ #specify the name of the output file #figure will be saved to the file #instead of be displayed on the screen
plot functions built-in functions define your own function sin, cos, exp, log, log10, sqrt, norm, abs, etc. define your own function +, - , * , / , etc.
plot functions Examples gnuplot> f(x)=5*x*2+1 gnuplot> plot f(x)
plot functions Examples gnuplot> plot 3*x+5*sin(x)
plot functions Examples gnuplot> splot sin(x)+sin(y)
add text to plot set title “A Figure” #set the title of the figure set xlabel “time” #set the x-axis label as time set label 1 “This is a label1” #specify the name of a label set label 1 at graph 0,0 #specify location of label 1, using graph coordinate #0,0 is bottom left and 1,1 is top right set label 2 “This is label2” at 3.14, 0 #place the label at a particular x-y coordinate
add shape to plot gnuplot> set object 1 rect from 0,0 to 1,4 gnuplot> plot [-5:5] x, -3+sin(x*5)/x lt 3 lw 3
plot data file datafile plot script # using 1:2 $ cat plotdata.txt 3.0 .98 3 4 5.3 .97 5 23 2.7 .8 2 18 5.1 .95 4 15 4.0 .7 4 17 4.4 .84 5 20 5.1 .94 3 13 4.5 .89 5 9 5.7 .94 5 1 datafile="./plotdata.txt" set terminal png set output "points.png" plot datafile using 1:2 with points pause -1 "Hit any key to continue” # using 1:2 # use the 1st and 2nd columns # with points # use points to plot data
plot data file result
plot data file plot with connected points datafile="./plotdata.txt" set terminal png set output “linespoints.png" plot datafile using 1:2 with linespoints pause -1 "Hit any key to continue”
plot data file result
plot data file plot with all three columns datafile="./plotdata.txt" set terminal png set output “linespoints.png" splot datafile using 1:2:3 with linespoints pause -1 "Hit any key to continue”
plot data file result
multiplot plot in the same figure plot f1(x), f2(x), ... Multiplot: plot in multiple subfigures set multiplot layout <rows>, <cols> unset multiplot
multiplot example gnuplot> set multiplot layout 1,2 multiplot> plot sin(x) multiplot> plot cos(x) multiplot> unset multiplot
References Find the demos , tutorials and documents at http://www.gnuplot.info/ Visualize your data with gnuplot http://www.ibm.com/developerworks/linux/library/l-gnuplot/ Display data on webpages http://www.ibm.com/developerworks/aix/library/au-gnuplot/index.html
Labs Lab 4 DUE: 11:59pm, Tuesday, April 14, 2015 http://web.cse.ohio-state.edu/~zhengm/teaching/cse4251sp15/CSE4251%20Lab%204.htm Lab 5 DUE: 11:59pm, Tuesday, April 28, 2015 (Late submission will not be accepted ) http://web.cse.ohio-state.edu/~zhengm/teaching/cse4251sp15/CSE4251%20Lab%205.htm