GNUPLOT

- not so Frequently Asked Questions -

update 2004/9/15

About Parametric Functions

use of parameters

In the usual 2-dimensional plot of gnuplot, the Y coordinate is expressed by y=f(x), however you can also use a parametric expression which uses the parameter t,

x = f(t)
y = g(t)

With this expression, more complicated functions can be plotted with gnuplot. Note that the 3-dim. plot with two parameters u,v is given in the spherical harmonics section.

First of all, you need to use the command set parametric to tell gnuplot that the function is defined by a parameter. Then, the plot command followed by a function f(t) which is the X-coordinate and a function g(t) for Y-coordinate, is give like, plot f(t),g(t) .

up

to draw a vertical line

The most simple but it is impossible to express by the y=f(x) form is a vertical line which is x=const. This function can be written as:

x=const
y=t

with the parameter t, when t is varied. The range of t is controlled by the command set trange .

gnuplot> set parametric

dummy variable is t for curves, u/v for surfaces
gnuplot> const=3
gnuplot> set trange [1:4]
gnuplot> set xrange [0:5]
gnuplot> set yrange [0:5]
gnuplot> plot const,t
sampleQ2.1

In this case the vertical line is draw at x=3. Since we used set trange [1:4] , the range of this truncated line is from 1 to 4. If trange not set, the vertical line is drawn from the bottom to top border lines.

up

to draw a circle, polygons

The parametric expression of a circle is

x=sin(t)
y=cos(t)

and the circle can be drawn if one changes the t parameter from 0 to 2pi. The graph is "squared" here, and the t range is given by an option of plot command.

gnuplot> set parametric

dummy variable is t for curves, u/v for surfaces
gnuplot> set size square
gnuplot> set xrange [-1:1]
gnuplot> set yrange [-1:1]
gnuplot> plot [0:2*pi] sin(t),cos(t)
sampleQ3.1

The parameter t is not changing continuously, and actually this is controlled by the value which is set by the set samples command. The default value is 100. In the case of set samples 8 , gnuplot generates eight t values from zero to 2*pi, and the graph becomes a regular heptagon. If you need a regular N-gon, just type set samples N+1.


sampleQ3.2

The 2-dim. parametric representation is convenient to draw a function which is in a polar coordinate. The 2-dim. polar coordinate has two variables which are radius r and angle theta. The gnuplot parameter t is for the theta, and the radius r is expressed by a function of angle, namely r(t). A (x,y) coordinate is given by

x=r(t)*cos(t)
y=r(t)*sin(t)

The circle is a special case of which r(t)=const. When the radius of circle is proportional to t, you get a spiral.

gnuplot> set xrange [-10*pi:10*pi]
gnuplot> set yrange [-10*pi:10*pi]
gnuplot> plot [0:10*pi] t*sin(t),t*cos(t)
sampleQ3.3

The following example shows r(t)=const*(1+cos(t)), which is called Cardioid.

gnuplot> set parametric

dummy variable is t for curves, u/v for surfaces
gnuplot> r(t) = 1+cos(t)
gnuplot> plot [0:2*pi] r(t)*cos(t),r(t)*sin(t)
sampleQ3.4
up

exchange X and Y-axes

Functions are normaly expressed by y=f(x), but the parametric expression allows us to make a graph of x=f(y). The y values are the same as t, and the x values are calculated with a function of f(t).

gnuplot> set parametric

dummy variable is t for curves, u/v for surfaces
gnuplot> c=2*pi
gnuplot> set size square
gnuplot> set trange [-c:c]
gnuplot> set xrange [-c:c]
gnuplot> set yrange [-c:c]
gnuplot> plot c*sin(t),t with lines, t,c*cos(t) with impulses
sampleQ4.1

Two functions are shown, one (green stripe) is y=2pi*cos(x), and the other (red solid line) is x=f(t)=2pi*sin(y).

The option with impulse draws a vertical line from the Y=0 axis. If you use with impulses for the red curve which is x=2pi*sin(y), you still get a vertical stripe, not a horizontal one.


up