You are here


Gnuplot Help

Obtaining Gnuplot

For PC : Download gnuplot.

For Mac OS X: First download and install XQuartz. Then install gnuplot for mac os x

Plotting Functions with Gnuplot

Let's try a few examples: To plot the function sin(x)

gnuplot>  plot sin(x)

To plot the functions sin(x) and cos(x) together

gnuplot> plot sin(x), cos(x)

To plot the function y(x)=3*x + 5

gnuplot> plot 3*x + 5

Another way to plot the function y(x)=3*x + 5

gnuplot> y(x) = 3 * x + 5

gnuplot> plot y(x)

To make sure y(x) has been defined use

gnuplot> show functions
User-Defined Functions: y(x) =3*x+5

Plotting Data

To plot a data file called "bignums.txt"

gnuplot> plot "bignums.txt"

Note: the double quotes are required! To plot the function y(x) = 3*x**2 - 5 * x + 2 from x= -3 to x= 6.4

gnuplot> y(x) = 3*x**2 - 5 * x + 2

gnuplot> plot [-3:6.4] y(x)

You can use the show functions command to check that the previous definition of y(x) was overwritten.

To plot a previously-made histogram called "my_histo.txt"

gnuplot> plot "my_histo.txt" with boxes

If you were plotting the file "myfile.txt" between limits with impulses.

gnuplot> plot [-5:14][0:6] "myfile.txt" with impulses

gnuplot> show term

Remember your terminal type!

Making Contour Plots

It is a bit tricky to make gnuplot do contour plots. Your data should be in the form

x1  y1  z1

x2  y2  z2

x3  y3  z3


with no blank lines. To make a contour plot with 10 levels of an (x y z) dataset called chibig.txt, do the following:

gnuplot> set parametric

gnuplot> set dgrid3d

gnuplot> set contour base

gnuplot> set nosurface

gnuplot> set view 0,0

gnuplot> set cntrparam levels auto 10

If you want more or less than 10 levels, change the number in the preceding line to whatever you want.

To plot the contour plot use

gnuplot> splot "chibig.txt" with lines

note that the command is "splot" (for surface plot), not plain old "plot". We've tested this on all 3 platforms and it works identically, so it should work for you. To print, use the method described below.

If you really care, you can enter "help set" to find out more about all those options you typed in.

There is a problem with the memory management in gnufit for the pc, and you may occasionally get "out of memory for isoline surfaces", or something like that, when making a bunch of contour plots under Windows 3.1. if you get this error quit gnufit and try it again, or switch to DOS (instead of a DOS window under Windows), which seems to do better.

Using gnuplot for fitting

Gnuplot has a built-in fitting routine using the Marquardt-Levenberg algorithm. This is quite fast and robust as fitting routines go. It will perform linear and non-linear fits for most any analytical function. To fit your data to a function y(x) first define the function:

gnuplot> y(x) = a*x**2 + m*x + b

Notice the FORTRAN convention of using double asterisks to achieve exponents. Next give some starting values for the parameters. They must include a decimal point!

gnuplot> a = 1.0

gnuplot> m = 5.0

gnuplot> b = 2.0

If you enter anything like

gnuplot> a = 1 ****** wrong *****

the program will blow up (that is, it will not converge and complain about a "Singular Matrix"). Then the command to fit the function y(x) to a datafile called "pair0.txt" is

gnuplot> fit y(x) "pair0.txt" via a, m, b

followed by lots of output. If you have entered "good" initial guesses for the parameters, then the fit should converge in just a few seconds. The results will appear on the screen and will be appended to a file called "fit.log". When reporting results, make sure you are looking at the correct section of "fit.log", as it can get confusing.

Printing from within gnuplot (to a Postscript file)

gnuplot> set term postscript

gnuplot> set output ""

gnuplot> replot 

The plot will be saved to the file "".

gnuplot> set output

To reset the output to the screen use

gnuplot> set term X11

or to vttek, tek40xx, or whatever your terminal type was. To send the PostScript file to the printer use

gnuplot> !lp ""