Brief Gnuplot tutorial

09/26/05

Gnuplot is simple, easy to script, free, but somewhat limited plotting package. It is available for GNU/Linux, Solaris, Windows, and OS X. It is good for quick plotting and is one of the best packages for 3d surface plotting. It is not suitable for heavy data analysis without using other tools along with it. Two good tutorials are at: http://www.cs.uni.edu/Help/gnuplot/ and http://www.duke.edu/~hpgavin/gnuplot.html.


First we will use gnuplot interactively. Type gnuplot in an xterm to start it. Now just type some function:
plot sin(x)
A plot should show up on the screen.
Obviously there is a lot more gnuplot can do. But first lets see how to save this quick plot to a file.

set output 'plot.ps'
set terminal postscript color solid enhanced
replot


If you would prefer a png:


set output 'plot.png'
set terminal png color
replot


Now for the more powerful and useful (in my opinion anyway) method of using gnuplot. It is possible to type all of your gnuplot commands into a script and the run that by typing: gnuplot scriptname and the prompt or by typing load 'scriptname' while in gnuplot. We can use this to show some more things we can do in a plot.


Here is an example data file we can use. It also shows how poorly I can come up with a set of random numbers.

Open a file (call it whatever you want) with your text editor. We will put our gnuplot commands there. The first thing to put in is the filename for the output and the terminal as we did in the interactive example above. We can leave it just plotting to the screen for now.

Put a label on the x axis: set xlabel "text"

Now do something similar to put a label on the y axis. This is the general form of nearly all options for gnuplot: set optionname value. Now set the title value to something in the same way.

Now we want to tell it to plot the data in our test file. Put the line plot 'test.dat' in your file and save it. The plot line must come after you set all your options if you want the option to effect the plot.

Now open gnuplot and type load 'filename' where filename is the name of your script. If should show you a plot of the data on the screen.

Now that we made that plot, let us go back to the script and see what else we can do.

We can change the scale of the plot with set xrange [low:high]. So set the x and y ranges of your plot to something else and run the script again.

With just a few points on the page, it is hard to see what is going on. We ca draw lines between them by adding the line set style data lines to the script. There are other choices that you can look up on the gnuplot page.

Now lets do something a bit more interesting with the plotting. By default, gnuplot just plots the first two columns of data. But maybe we want to plot column 2 and 3. Or 2 and some variation on 3. Go back to your script. change the plot line to:. plot "test.dat" using 2:3 title "my data"

That line shows us two things: how to change which columns we plot, and how to put our own text in the legend.

Now change the line to plot "test.dat" using 2:($3*2) title "my data". This shows us how to manipulate the data in the columns. Finally, lets put more than one line on the plot.

plot "test.dat", sin(x) We can put as many different lines on a plot as we want, each separated by a comma. If it take more than one line in your script (and you really should use a text editor that can do word wrapping), use a \ at the end of the line that is to be continued.

Some other options you might be interested in are set nokey to turn the legend off and set logscale to have logarithmically scaled plots.


Gnuplot is one of the easiest programs to do 3d plotting in. All of the options are the same (except there are now z versions of them to go along with x and y). The plot command is replaced with splot. If you are plotting from a data file, you obviously need a minimum of 3 columns instead of two, but otherwise splot behaves just like plot. Try plotting some functions with splot.

splot sin(x)*cos(y) gives a decent example.

There are a few options you should be aware of. You should try some of the now either in a script or interactively using the replot command. Remember, each set command has a corresponding unset command and vice versa.

set contour is used to turn contours on the bottom of the plot.

unset surface turns the surface off so you are just plotting points in a three dimensional field.

set hidden cause the plot to hide lines that would be blocked by other lines (this is easier to understand if you just try it).

Finally, set view 0,0 lets you choose what location you view the plot from (where you change the numbers to vary what angle you view the plot from).


Here is a brief example on how to make a plot with four different plots on one page. This is not easy to do in some other plotting packages.


Back to Intro UNIX pt. 2


Back to John C. Vernaleo's home page