# 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)
```

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)
```

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)
```

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!

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

1. […] de geometría analítica en el espacio. Me ha impresionado las salidas vistosas que ha obtenido Hagen Wierstorf con su uso. Sin embargo, es muy laboriosa la colocación de etiquetas en los sitios adecuados; […]

2. Bo Thidé says:

Hi,

The first 3D parametric example, drawing a (piece of a) sphera does not work. When I try to run it I get the error messge:

“line 9: undefined variable: r”

There is no way I can reproduce Fig. 4. What am I doing wrong?

Bo

3. hagen says:

Hi Bo,

you have to run the complete script that is linked in the caption of Fig. 4. Then it should work as `r = 1.0` is specified in there. The code examples in the text of the blog entry are only for illustrating the principles they normally do not work via copy and paste.

4. […] parametric plots […]

5. mw says:

Hi there; I have used gnuplot for years, but now I want to plot a 3D *curve* – that is, a curve thru 3D space. NOT a ‘surface’. I’ve been told that gnuplot will only do that with functions (or probably, expressions), but not with data files.
Is this true? Can gnuplot not plot 3D parametric curves from data?
Thanks, mw

6. hagen says:

It’s possible from data files as well, have a look at http://lowrank.net/gnuplot/plot3d2-e.html