Plotting Mathematical functions with Gnuplot
January 18th, 2008Complex math always scares me. Mathematical functions written on the paper never made any sense to me. But i used to find it interesting to plot these functions and see the shapes they produce.But plotting them with pencil and paper was painful and took lot of time. Later after I learnt some elementary programming in C, I generated datasets with simple programs and tried using
putpixel(x,y) like functions, to plot them on screen. Even that wasn’t easy and dint get smooth curves.
Now i am trying to do the same exercise using a powerful tool of the GNU tool chain – Gnuplot. Its available free with almost all Linux distros or can be downloaded for free from here. Debian users can use
sudo apt-get install gnuplot
Getting started with gnuplot
gnuplot is extremely user/programmer friendly. Just type ‘gnuplot’ to get the gnuplot shell. I wanted to see my favorite Sine wave first.
gnuplot> plot sin(x)
Thats it and i got my first function plotted on gnuplot. It cant get anymore simple..!
Saving output to file
Next I wanted to save the graph as an image file.
gnuplot> set terminal png
gnuplot> set output “sine.png”
gnuplot> replot
Now the output is dumped into “sine.png” in the current directory.
‘replot’ simply repeats the last ‘plot’ call.
Plotting multiple functions in the same graph
Intuitive ways work perfectly on gnuplot. To plot sine and cos waves on the same graph :
gnuplot> plot sin(x),cos(x)
OR
gnuplot> plot sin(x)
gnuplot> replot cos(x)
replot adds its arguments to the previous plot’s argument list and then calls plot again.
Plotting parametric equations
1. Line
x = constant
y = t
gnuplot> set parametric
gnuplot> plot 3,t
gnuplot> plot t,3
2. Square
Basically 2 vertical and 2 horizontal striaght lines.
gnuplot> set parametric
gnuplot> set xrange[-2:8]
gnuplot> set yrange[-2:8]
gnuplot> set trange[0:4]
gnuplot> plot 0,t
gnuplot> replot 4,t
gnuplot> replot t,0
gnuplot> replot t,4
3. Circle
Circle of radius r and centered at (a,b) :
x = r * Cos(t) + a
y = r * Sin(t) + b
gnuplot> set parametric
gnuplot> set angle degree
gnuplot> set trange [0:360]
gnuplot> set size square
gnuplot> r = 4
gnuplot> plot r*cos(t), r*sin(t)
4. Spiral
In the circle’s equation, instead of keeping radius constant, if we make it a function of t, we get a spiral (when r(t) = t)
x = r(t) * Cos(t) + a
y = r(t) * Sin(t) + br(t) = t
gnuplot> set parametric
gnuplot> set trange [0:10*pi]
gnuplot> set xrange[-10*pi:10*pi]
gnuplot> set yrange[-10*pi:10*pi]
gnuplot> set samples 1000
gnuplot> plot t*cos(t), t*sin(t)
5. Cardioid
As its name indicates, Cardioid is a heart shaped curve. In spiral’s parametric equation, if we put r(t) = 1 + Cos(t) , the result is a cardioid.
gnuplot> set parametric
gnuplot> r(t) = 1 + cos(t)
gnuplot> plot r(t)*cos(t), r(t)*sin(t)
Plotting Styles
plot command can take different options like lines, impulses, boxes, points, linespoints etc
gnuplot> plot sin(x) with impulses
3D Plotting
splot is the command used for 3-dimensional plotting.
gnuplot> splot sin(x)
Click and drag with mouse to rotate the figure 3-dimensionally.
i. Sphere
Parametric equations are:
x = cos(u)*sin(v)
y = sin(u)*sin(v)
z = cos(v)
gnuplot> set parametric
gnuplot> set urange [0:2*pi]
gnuplot> set vrange [0:pi]
gnuplot> set isosample 40
gnuplot> splot cos(u)*sin(v),sin(u)*sin(v),cos(v)
ii. Cone
Parametric equations are:
x = r cos(t)
y = r sin(t)
z = t
gnuplot> set parametric
gnuplot> set urange [-2:2]
gnuplot> set vrange [0:2*pi]
gnuplot> set isosample 40
gnuplot> set view 75,30
gnuplot> splot u*cos(v),u*sin(v),u
Parametric equations for 3d figures were obtained from Introduction to gnuplot for Vector Calculus

hello i found your posting very useful . thanks. is there a way to save the graph to a file and taking input from a file? thanks.
Hi Pankaj, there is a section “Saving output to file” in my post itself..!
Reading input from file is pretty straight forward. Just put all the gnuplot commands in a file, say x.plot and then
“gnuplot x.plot”
I saw your long posting regarding Gnuplot, and thought you might be interested to know that there is now a book on it: “Gnuplot in Action”. You can pre-order it directly from the publisher: Manning: Gnuplot in Action.
If you want to learn more about the book and the author, check out my book page at Principal Value – Gnuplot in Action.
Let me know if you are interested in a review copy.
Quick correction: Despite its name, Gnuplot is not part of the “GNU Toolchain”. Check the FAQ for an explanation of the name. FAQ – Name
Hi, I´m new in using gnuplot, and I really find it useful, even though I don’t know much of the commands. I have a doubt, when I want to graph spheres, cones, hyperboloids, and all that kind of figures, do I have to introduce them in there parametric equations? Is there a way to introduce them in cartesian equations, were I can use x,y,z? I hope you can answer as soon as possible!