Gnuplotting

Create scientific plots using gnuplot

June 4th, 2010 | 6 Comments

If one have a coordinate system with n-dimension, then one of the dimensions can be expressed by the n-1 other dimensions, e.g. z = f(x,y).
But if you want to plot functions that are defined in polar coordinates, e.g. a sphere, they are complicated to define with z = f(x,y). But Gnuplot offers you a way to handle this type of functions by using its parametric mode. In parametric mode the functions are expressed in angular coordinates t or u,v dependend on the dimensions of your plot.

2D case

In the 2D case we have only one free dimension:

y = f(x)   =>   x = fx(t), y = fy(t)

In Fig. 1 we see the connections between the angular coordinate t and radius r and x,y that is given by

x = r cos(t)
y = r cos(π/2-t) = r sin(t)
Parametric 2D plot

Fig. 1 Connection between Gnuplots parametric variable t and x,y (code to produce this figure)

Using the result from above it is very easy to plot a circle:

set parametric
set trange [0:2*pi]
# Parametric functions for a circle
fx(t) = r*cos(t)
fy(t) = r*sin(t)
plot fx(t),fy(t)
Circle

Fig. 2 Plot of a circle using Gnuplots parametric mode (code to produce this figure)

3D case

In three dimensions we have the case:

z = f(x,y)   =>   x = fx(u,v), y = fy(u,v), z = fz(u,v)

In Fig. 3 we see the connection between the two angular variables u (that is t in the 2D case), v and the radius r:

x = r cos(v) cos(u)
y = r cos(v) sin(u)
z = r sin(u)
Parametric 3D plot

Fig. 3 Connection between Gnuplots parametric variables u,v and x,y,z (code to produce this figure)

Using the parametric variables u,v it is very easy to draw a sphere or a piece of a sphere:

set parametric
set urange [0:3.0/2*pi]
set vrange [-pi/2:pi/2]
# Parametric functions for the sphere
fx(v,u) = r*cos(v)*cos(u)
fy(v,u) = r*cos(v)*sin(u)
fz(v)   = r*sin(v)
splot fx(v,u),fy(v,u),fz(v)

The result is shown in Fig. 4. Note that we have to use 3.0/2, because 3/2 is 1 for Gnuplot!

Sphere

Fig. 4 Plot of a sphere using Gnuplots parametric mode (code to produce this figure)