GNUPLOT Brief Intro notes taken by Rudy Magyar from a brief introduction by Neepa Maitra 4/1/0 This guide is a crash course in the use of GNUPLOT. It is set up to serve both as a tutorial and a reference card. More detailed information about the syntax and features of each command is available through the GNUPLOT help program. Before you do anything else, make sure GNUPLOT is installed on your machine. If not, then this guide probably won't help much. Run xwindows or some other GUI, and open up an xterm window. To do this, type: prompt> xterm -e gnuplot & You will get a separate window with a gnuplot prompt. First, we'll try to plot some simple functions. gnuplot> plot sin(x)*x**3 plots $x^3 * \sin(x)$ and opens a display window for the plot. gnuplot> plot[-20:20] sqrt(sin(x)) plots $\sqrt{\sin(x)}$ with an x-axis from -20 to 20. gnuplot> plot[0:10][0.5:1] real(sqrt(sin(x))) plots the real part of $\sqrt{\sin(x)}$ from x = 0 to 10 and shows y = 0.5 to 1. Most of the stuff you'll want to plot will come from data files. Let's see how to work with these. gnuplot> plot "my_file.dat" u 1:2 plots the data in "my_file.dat". The number before the colon, one, tells us that the data in column one will be our independent (i.e. x axis) set. The number after the colon, two, tells us that column two will be our dependent set (i.e. f(x)). This won't work unless you have created and saved in the working directory some file "my_file.dat" beforehand. gnuplot> plot "my_file.dat" u 1:2 w li does the same as above but uses lines to connect points. To choose the color of a line use: w li 1. The number determines the color. 1 is red, 2 is green, 3 is blue, etc. gnuplot> plot "file_one.dat" u 1:2 w li 1, "file_two.dat" u 1:2 w li 2 superimposes two plots, "file_one.dat" and "file_two.dat". The first plot is red and the latter green. Don't worry if you forget which is which. The plot will include a legend which gives the color code. You'll probably want your output to be a bit more readable, so let's ad titles and a legend. gnuplot> ti "new_function" w linespoints Associates the last plotted function with the name "new_ function" in the legend. gnuplot> set title "testing gnuplot" titles the graph. Some other things you might want to adjust are the number of marks on the axes, the legend location, etc. gnuplot> set xtics 1 Sets the number of ticks on the x axis. gnuplot> set key bottom moves the legend to the bottom. We could choose left, right. etc. gnuplot> set xlabel "t axis" labels the x axis. gnuplot> set size 0.5,1 shrinks the entire graph. gnuplot> set noxtics gets rid of the ticks on the x axis. gnuplot> replot update the current graph. To facilitate reproducibility and to reduce the number of key strokes, it is nice to work with scripts, lines of GNU code printed in a text file and run by GNU. gnuplot> load "my_script.gnu" loads and runs a text gnu script. Occasionally, you might want to plot several functions on one graph. You can make your scripts more readable if you uses backslashes to extend your input lines. plot "file_one.dat" u 1:2 ti "the first file" w li 2, \ "file_two.dat" u 1:2 ti "the next file" w po 5 superimposes two plots, "file_one.dat" and "file_two.dat". The first plot shows up as a green curve, and the latter will be a blue series of points. If you want to include several graphs on one output, you'll have to use multiplot mode. gnuplot>set multiplot gnuplot>set size 0.5,0.5 gnuplot>set origin 0,0.5 gnuplot>plot "a.txt" u 1:2 gnuplot>set origin 0.5,0.5 gnuplot>plot "b.txt" u 1:2 gnuplot>set origin 0,0 gnuplot>plot "c.txt" u 1:2 gnuplot>set origin 0.5,0 gnuplot>plot "d.txt" u 1:2 gnuplot>set nomultiplot gnuplot>set size gnuplot>set origin sets up four plots, "a.txt", "b.txt", "c.txt", and "d.txt" on one output. gnuplot> set term postscript; set out "file.ps" tells gnu to get ready to write the graph as a postscript to "file.ps". If you put portrait after the postscript, you will be in portrait mode. You can also set term eps 22 or 14 which enable you to make encapsulated postscript files. gnuplot> replot makes the postscript. gnuplot> set term X11 gets you back to screen mode. gnuplot> save "my_work.gnu" saves what you have done in gnuplot. Note pi stands for "pi". Finally, I report some miscellany. gnuplot> set pointsize 0.7 Sets the point size. gnuplot> plot "my_crazy_results.dat" u 1:($2**2) Plots the data in the file "my_crazy_results.dat". The first column is x, and the second column is squared and the plotted. gnuplot> print V(0.7) I have no idea what this does. Now, I'm confused, but no fear. When you are totally confused like I am, GNU might help. gnuplot> help provides help and support. At the end of the day, you'll be sick of GNU. gnuplot> quit gets you out of the gnuplot environment.