Introduction to Gnuplot

Gnuplot is a useful tool for plotting data, and is available on Ugrad.  Start it up by typing "gnuplot".  It might be more convenient to do this in a new window, which you can start using "xterm &".

Note: the files used in this tutorial are available in ~afrith/phys2020, so you can get them using "cp ~afrith/phys2020/*.* ."

Functions

Gnuplot can plot mathematical functions, which you can write in the same way you would write expressions in C.

Example:

gnuplot>plot sin(x) + sin(x/2)

view output

Plotting Data

Gnuplot can plot data in text files, using the plot and splot comands..  Data should be in columns, and data with more than one column should be delimited by either tabs or spaces, not commas.

Example:

gnuplot>plot "1d.dat"
view output
gnuplot>plot "2d.dat"
view output

will plot the data in the file quoted.  Two columns of data will be interpreted as x and y axis values, one column of data will be interpreted as y axis values, with the line number of each y value being used as the corresponding x value.

You can also override these defaults and choose which columns to plot.

Example:

gnuplot>plot "3d.dat" using 1:2, "3d.dat" using 1:3
view output

will plot the first column as x values and the third column as y values

Gnuplot can also plot on the z axis using splot.

Example:

gnuplot>splot "3d.dat"
view output

Type "help splot" for more information.

Formatting

Example:

gnuplot>plot "2d.dat" title 'sin(x) + sin(x/2) + sin(x/4)'
gnuplot>plot "2d.dat" title 'sin(x) + sin(x/2) + sin(x/4)' with linespoints
view output

"Set"

Example:

gnuplot>set xlabel 'x'
gnuplot>replot
gnuplot>set xrange [0:2]
gnuplot>replot
 
view output

Type "help set" for more information on things you can change.

Scripting

To save typing in the same commands each time you want to plot some data, you can write a series of gnuplot commands in a file, and then use the load command to run those commands.  Lines starting with the "#" character are comments.

Example:

gnuplot>load "script"