Download presentation
Presentation is loading. Please wait.
Published byRandall Farmer Modified over 8 years ago
1
Using GNUPlot on LONI Resources B. Estrade
2
Objectives ● learn to use many features of gnuplot, including: – simple plotting – 3d plotting – working with data files – automating gnuplot – sources of more information
3
Note ● much of the material for this presentation was borrowed from the extremely useful and comprehensive gnuplot guide at: http://t16web.lanl.gov/Kawano/gnuplot/index-e.html
4
Exercises ● When applicable, I will stop and ask each participant to try an example for themselves
5
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
6
Getting Started ● log into a remote LONI resource, making sure X11 forwarding is enabled qb1.loni.org ● for more info, please see https://docs.loni.org/wiki/X11_Forwarding ● start gnuplot interactively by typing “gnuplot” into the shell prompt
7
The gnuplot Shell ● provides an interactive interface to gnuplot ● provides methods to output plots in various formats ● provides access to underlying user land shell
8
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
9
Your First Plot uses the plot command
10
Your Second Plot uses the replot command to retain the 1 st plot
11
Your Third Plot uses the replot command to retain the 1 st and 2 nd plot
12
Axis Labels uses the set xlabel and set ylabel commands to label the X and Y axis, respectively
13
Axis Ranges uses the set xrange and set yrange commands to define the X and Y ranges, respectively
14
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
15
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
16
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
17
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
18
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
19
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 14620 Jun 2 15:19 sinplot.ps !
20
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
21
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
22
Working With Data Sets # X Y1 Y2 Y3 -1.0000 0.0000 0.0000 1.0000 -0.9000 0.5700 1.1769 0.7150 -0.8000 1.0800 1.4400 0.4600 -0.7000 1.5300 1.4997 0.2350 -0.6000 1.9200 1.4400 0.0400 -0.5000 2.2500 1.2990 -0.1250 -0.4000 2.5200 1.0998 -0.2600 -0.3000 2.7300 0.8585 -0.3650 -0.2000 2.8800 0.5879 -0.4400 -0.1000 2.9700 0.2985 -0.4850 0.0000 3.0000 -0.0000 -0.5000 0.1000 2.9700 -0.2985 -0.4850 0.2000 2.8800 -0.5879 -0.4400 0.3000 2.7300 -0.8585 -0.3650 0.4000 2.5200 -1.0998 -0.2600 0.5000 2.2500 -1.2990 -0.1250 0.6000 1.9200 -1.4400 0.0400 0.7000 1.5300 -1.4997 0.2350 0.8000 1.0800 -1.4400 0.4600 0.9000 0.5700 -1.1769 0.7150 1.0000 0.0000 -0.0000 1.0000 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: http://t16web.lanl.gov/Kawano/gnuplot/datafile2-e.html#7.2
23
Calculations Using Column Data # X Y1 Y2 Y3 -1.0000 0.0000 0.0000 1.0000 -0.9000 0.5700 1.1769 0.7150 -0.8000 1.0800 1.4400 0.4600 -0.7000 1.5300 1.4997 0.2350 -0.6000 1.9200 1.4400 0.0400 -0.5000 2.2500 1.2990 -0.1250 -0.4000 2.5200 1.0998 -0.2600 -0.3000 2.7300 0.8585 -0.3650 -0.2000 2.8800 0.5879 -0.4400 -0.1000 2.9700 0.2985 -0.4850 0.0000 3.0000 -0.0000 -0.5000 0.1000 2.9700 -0.2985 -0.4850 0.2000 2.8800 -0.5879 -0.4400 0.3000 2.7300 -0.8585 -0.3650 0.4000 2.5200 -1.0998 -0.2600 0.5000 2.2500 -1.2990 -0.1250 0.6000 1.9200 -1.4400 0.0400 0.7000 1.5300 -1.4997 0.2350 0.8000 1.0800 -1.4400 0.4600 0.9000 0.5700 -1.1769 0.7150 1.0000 0.0000 -0.0000 1.0000 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: http://t16web.lanl.gov/Kawano/gnuplot/datafile2-e.html#7.2
24
Different Data Sets in a Single File # X Y Yerror 1.0 1.2 0.1 2.0 1.8 0.1 3.0 1.6 0.1 1.1 0.8 0.2 2.1 0.3 0.2 3.1 1.0 0.2 1.2 1.5 0.3 2.2 2.3 0.3 3.2 3.1 0.3 gnuplot> plot "test.dat" using 1:2 with lines data from: http://t16web.lanl.gov/Kawano/gnuplot/datafile2-e.html#7.2 test.dat 2 blank lines b/w data sets
25
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
26
3D Plotting ● plotting in 3D requires the “splot” command, which is analogous to the familiar “plot” command gnuplot> splot(x**2)*(y**2)
27
3D Plotting ● the following moves the z-axis up to the xy plane gnuplot> set ticslevel 0 gnuplot> splot(x**2)*(y**2)
28
Rotating View ● when a 3D plot is created, one may “drag” the image around to adjust the view ● (see live example)
29
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)
30
3D Data Sets # X Y Z 0 0 0 0 1 1 0 2 4 0 3 9 0 4 16 0 5 25 1 0 1 1 1 2 1 2 5 1 3 10 1 4 17 2 0 4 2 1 5 2 2 8 2 3 13 3 0 9 3 1 10 3 2 13 data from http://t16web.lanl.gov/Kawano/gnuplot/datafile-e.html#3dim gnuplot> splot "test3.dat" using 1:2:3 with lines test3.dat
31
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
32
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
33
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
34
References ● http://t16web.lanl.gov/Kawano/gnuplot/index-e.html
35
Additional Resources ● http://docs.loni.org ● http://t16web.lanl.gov/Kawano/gnuplot/gallery/index-e.html ● http://t16web.lanl.gov/Kawano/gnuplot/index-e.html ● http://t16web.lanl.gov/Kawano/gnuplot/datafile-e.html ● Google (keywords, “gnuplot tutorial”, “gnuplot how to”, etc) ● gnuplot's build-in help (type “help” at the interactive prompt)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.