Gnuplot Lab
Introductory lab for gnuplot
Overview
The goal of this lab is to introduce you to GnuPlot. GnuPlot is an interactive data and function plotting utility that is run from the command line. You will learn how to:
- Plot a simple function
- Limit the range of the plot
- Add a title
- Print to a file that can be used with LaTex
Disclaimer: much of the material in this lab is liberally borrowed from other online sources.
Lets start by seeing some demonstration output from GnuPlot. In particular, make sure to look out the surfaces and contours.
To start GnuPlot in Windows, find the GnuPlot folder in Program Files -- Bin. Click on the icon wgnuplot. You should see something like this Screenshot.
Simple plots
plot is used to plot 2-d functions. To plot functions, type plot [function]. To see some of the capabilities of plot, we’ll start with a simple example and then modify it.
gnuplot> plot x**0.5
Restricting range
Let us say we want to change the range of x. Ranges are specified using the notation [min:max]. To specify only the min, use [min:] and to specify only the max, use [:max].
set xrange [0:25] replot
What happens if you do the following?
reset
Change the range in some other way.
Note that you can set the range at the very beginning by doing the following:
plot [0:25] x**0.5
As you noticed, replot executes the previous plot command. This can be very useful when you are composing a graph and need to keep changing it to add desired features. Additionally, replot lets you add more graphs. Try typing in:
replot 2*x**0.5
Making a title
Next, let us give titles to the graph (”My First Graph”) and the axes (x-axis is ”x,” and y-axis is ”U(x))”).
set title ”My first graph” set xlabel ”x" set ylabel ”U(x)” plot x**0.5
Making output for Latex
We can create output that is readable by latex.. There are multiple ways to create output for latex. This is the more fool-proof method.
set terminal postscript eps set output "figure.eps" replot
Find your output file "figure.eps" and save it to the same location as where your latex files are located (likely in My Documents). In Latex, open a new file and paste in the template.
Put the following in your latex document (in between begin and end document).
\begin{figure}[tbp]
\caption{Mean responses to NEP questions by method and class}\label{figure:GrpChar}
\begin{center}
%\fbox{\includegraphics[angle=90, width=1.0\textwidth]{GraphCompareLCCA}}
\fbox{\includegraphics[width=1.0\textwidth]{figure2.eps}}
\end{center}
\end{figure}
Now compile.Creating output in .tex format
Note: This doesn't work well on these computers. It may work on your computer. Do the following:
set terminal latex set output "Figure1.tex" replot
Now open this file in Latex. In the beginning, put:
\documentclass[12]{article}
\begin{document}
and at the end:
\end{document}
Now compile your document and view it. Cool, huh? If you want to send output back to the screen, do:
set terminal windows # in Windows set terminal x11 # in mac
Plotting 3-d surfaces
Suppose we want to plot some multivariate functions. Splot plots 3-d surfaces. Try the following:
splot (x*y)**0.5
This isn't quite what we want. Lets change the range:
splot [x=0:10][y=0:10] (x*y)**0.5
- Add a title and label the axis.
- Try changing the exponents and see how the function changes.
- Change the function to one with a minimum.
If you want the z axis to start at zero, try
set ticslevel 0
Contours
Lets add some contours. Try the following:
set contour base replot
Do these contours look like what you would expect from this function?
Go crazy
Experiment using Gnuplot. If you're not sure what you want to do, try the following:
- Plot level curves for the following (hint: if you want level curves only, try: set nosurface)
- z = x^2 + y^2
- z = x-y^2
- z = x + y
- Have GnuPlot generate the following
- Q = L^{1/4} K^{3/4}
- Q = L^{1/2} K^{1/2}
- Q = L^{7/8} K^{1/8}
- Q = LK
Installation
For whatever reason, I had difficulty installing this on my mac. Gnuplot needs either xwindows or aquaterm. I already had xwindows. What finally worked was:
sudo apt-get install gnuplot
Then from xwindows terminal, type
>gnuplot
Gnuplot opens up.
Note that the above did not work with fink install gnuplot. Based on some online reading, it looks like there may be a problem with the fink installation.
Windows documentation
http://gamma.ethz.ch/install/win9xnt/gnuplot/index.html
http://users.cse.ucdavis.edu/~cmg/courses/nlp/Software/Windows/py25install.html
Download site for gnuplot:
Select gp424win32.zip(Note: this is important b/c it contains all the other supporting materials that you need)
It didn't install to my program files once unzipped. Instead, go to bin and click on the wgnuplot graphic.
Instructions for installing cygwin: http://www2.warwick.ac.uk/fac/sci/moac/currentstudents/peter_cock/cygwin/part1/

