Using GNUPlot on LONI Resources B. Estrade
Objectives ● learn to use many features of gnuplot, including: – simple plotting – 3d plotting – working with data files – automating gnuplot – sources of more information
Note ● much of the material for this presentation was borrowed from the extremely useful and comprehensive gnuplot guide at:
Exercises ● When applicable, I will stop and ask each participant to try an example for themselves
What is gnuplot? ● a fairly complete, free graphing tool ● works on Linux and Windows, among other OSs ● provides interfaces for both interactive and batch plotting ● provides basic 3D plotting capabilities ● best of all, gnuplot is easy
Getting Started ● log into a remote LONI resource, making sure X11 forwarding is enabled qb1.loni.org ● for more info, please see ● start gnuplot interactively by typing “gnuplot” into the shell prompt
The gnuplot Shell ● provides an interactive interface to gnuplot ● provides methods to output plots in various formats ● provides access to underlying user land shell
Non-plotting Commands ● some useful built-in commands – gnuplot> pwd – gnuplot> cd ● to escape to the underlying user land shell, preface the command with an exclamation point; example: – gnuplot> !ls -lart – gnuplot> !cat somefile > someotherfile
Your First Plot uses the plot command
Your Second Plot uses the replot command to retain the 1 st plot
Your Third Plot uses the replot command to retain the 1 st and 2 nd plot
Axis Labels uses the set xlabel and set ylabel commands to label the X and Y axis, respectively
Axis Ranges uses the set xrange and set yrange commands to define the X and Y ranges, respectively
Command Recap (please try) 1)plot sin(x) 2)replot cos(x) 3)replot sin(x)*cos(x) 4)set xlabel “X-LABEL” 5)set ylabel “Y-LABEL” 6)replot 7)set xrange [0:5] 8)set yrange [-2:2] 9)replot
Line Types ● gnuplot uses the with keyword to specify the line type ● Line types supported by gnuplot: 1) lines 2) points 3) linespoints 4) impulses 5) dots 6) steps
Examples gnuplot> plot cos(x) with points gnuplot> plot sin(x) with impulses gnuplot> plot sin(x) with linespoints gnuplot> plot sin(x) with steps
Line Type Attribute ● Each line type has associated attributes, including color, style, and width; ● Additional examples: ● For more information and examples type “help with” at the gnuplot command prompt. gnuplot> plot sin(x) lt -1 with lines # adds black line gnuplot> replot cos(x) lt 0 with lines # adds white (dotted) line gnuplot> replot sin(x)*cos(x) lt 1 with lines # adds red line gnuplot> replot sin(x)/cos(x) lt 2 with lines # adds green line
Terminal Types in gnuplot ● gnuplot uses the current “terminal type” to determine in what way to output the current plot ● for example, in interactive mode gnuplot automatically sets the terminal type to be “x11” when started ● this is how we can see the plots even when they're created on a remote machine while in interactive mode
Saving Plots ● simply set the terminal type to the format you wish it save (e.g., postscript, jpeg, etc) ● the following example saves a plot to a postscript file: gnuplot> set terminal postscript Terminal type set to 'postscript' Options are 'landscape noenhanced monochrome blacktext \ dashed dashlength 1.0 linewidth 1.0 defaultplex \ palfuncparam 2000,0.003 \ butt "Helvetica" 14' gnuplot> set output "sinplot.ps" gnuplot> plot sin(x) gnuplot> ! ls -l sinplot.ps -rw-r--r-- 1 estrabd loniadmin Jun 2 15:19 sinplot.ps !
More Terminal Info ● reading the help on “terminal” provides a lot of good information about what types are supported ● to view the help, type in “help terminal” at the gnuplot prompt
Command Recap (please try) 1)plot cos(x) with points 2)plot sin(x) with impulses 3)plot sin(x) with points 4)plot sin(x) with linespoints 5)plot sin(x) with steps 6)plot sin(x) lt -1 with lines 7)replot cos(x) lt 0 with lines 8)replot sin(x)*cos(x) lt 1 with lines 9)set terminal postscript 10)set output “sin.ps” 11)replot 12)help terminal
Working With Data Sets # X Y1 Y2 Y gnuplot> plot "test.dat" using 1:2 with lines,\ "test.dat" using 1:3 with lines,\ "test.dat" using 1:4 with lines test.dat data from:
Calculations Using Column Data # X Y1 Y2 Y gnuplot> plot "test.dat" using 1:2 with points,\ "test.dat" using 1:($2*2) with points,\ "test.dat" using 1:(sqrt($2)) with points,\ "test.dat" using 1:(log($2)) with points test.dat data from:
Different Data Sets in a Single File # X Y Yerror gnuplot> plot "test.dat" using 1:2 with lines data from: test.dat 2 blank lines b/w data sets
Filtering Data ● gnuplot allows for shell commands to be embedded into plot commands ● this ability provides for a great amount of flexibility when plotting data gnuplot> plot "head -n 4 test.dat" using 1:2 with lines,\ "tail -n 3 test.dat" using 1:3 with lines,\ "cat test.dat |./filter.pl" using 1:4 with lines
3D Plotting ● plotting in 3D requires the “splot” command, which is analogous to the familiar “plot” command gnuplot> splot(x**2)*(y**2)
3D Plotting ● the following moves the z-axis up to the xy plane gnuplot> set ticslevel 0 gnuplot> splot(x**2)*(y**2)
Rotating View ● when a 3D plot is created, one may “drag” the image around to adjust the view ● (see live example)
Contours ● contours may be projected onto the z-axis gnuplot> set contour gnuplot> set cntrparam levels 10 gnuplot> set cntrparam levels incremental -1, 0.2, 1 gnuplot> set cntrparam levels discrete -0.2, -0.5, 0.2, 0.5 gnuplot> splot (x**2)*(y**2)
3D Data Sets # X Y Z data from gnuplot> splot "test3.dat" using 1:2:3 with lines test3.dat
Automating gnuplot ● gnuplot supports batch scripting, which allows the automating of tedious plotting commands #!/usr/bin/gnuplot -persist # set terminal set terminal x11 # plot a simple function plot sin(x) # # save as postscript file # set terminal postscript set output "test.ps" replot first line is like a shell script and denotes the application to use, including any options “-persist” tells gnuplot to keep the plot on the screen even after the script has finished executing set terminal to X11 plot function, will appear on screen save plot as a postscript file on the remote host a comment, ignored by gnuplot test.gp
Automating gnuplot ● save to “test.gp” ● To run: – make executable chmod 755 test.gp – run with,./test.gp ● Alternatively, gnuplot accepts STDIN: cat test.gp | gnuplot -persist or gnuplot -persist < test.gp
Putting It All Together #!/usr/bin/gnuplot -persist # set terminal set postscript # 2d data set output “2d-test.ps” plot "test.dat" using 1:2 with lines,\ "test.dat" using 1:3 with lines,\ "test.dat" using 1:4 with lines # 3d data set output “2d-test.ps” splot "test3.dat" using 1:2:3 with lines # filtered data set output “filtered-test.ps” plot "head -n 4 test.dat" using 1:2 with lines,\ "tail -n 3 test.dat" using 1:3 with lines,\ "cat test.dat |./filter.pl" using 1:4 with lines # advanced use shell escapes # --example requires ImageMagik's convert utility ! convert 2d-test.ps 2d-test.jpg # create jpg from ps ! convert 3d-test.ps 3d-test.jpg # create jpg from ps
References ●
Additional Resources ● ● ● ● ● Google (keywords, “gnuplot tutorial”, “gnuplot how to”, etc) ● gnuplot's build-in help (type “help” at the interactive prompt)