16.27 GNUPLOT: Display of functions and surfaces

This package is an interface to the popular GNUPLOT package. It allows you to display functions in 2D and surfaces in 3D on a variety of output devices including X terminals, PC monitors, and postscript and Latex printer files.

NOTE: The GNUPLOT package may not be included in all versions of REDUCE.

Author: Herbert Melenk.

16.27.1 Introduction

The GNUPLOT system provides easy to use graphics output for curves or surfaces which are defined by formulas and/or data sets. GNUPLOT supports a variety of output devices such as VGA screen, postscript, picTEX, MS Windows. The REDUCE GNUPLOT package lets one use the GNUPLOT graphical output directly from inside REDUCE, either for the interactive display of curves/surfaces or for the production of pictures on paper.

16.27.2 Command plot

Under REDUCE GNUPLOT is used as graphical output server, invoked by the command plot(...). This command can have a variable number of parameters:

Please note that a blank has to be inserted between a number and a dot, otherwise the REDUCE translator will be misled.

If a function is given as an equation the left-hand side is mainly used as a label for the axis of the dependent variable.

In two dimensions, plot can be called with more than one explicit function; all curves are drawn in one picture. However, all these must use the same independent variable name. One of the functions can be a point set or a point set list. Normally all functions and point sets are plotted by lines. A point set is drawn by points only if functions and the point set are drawn in one picture.

The same applies to three dimensions with explicit functions. However, an implicitly given curve must be the sole object for one picture.

The functional expressions are evaluated in rounded mode. This is done automatically, it is not necessary to turn on rounded mode explicitly.


plot(cos x);  
plot(s=sin phi, phi=(-3 .. 3));  
plot(sin phi, cos phi, phi=(-3 .. 3));  
plot (cos sqrt(x^2 + y^2), x=(-3 .. 3), y=(-3 .. 3), hidden3d);  
plot {{0,0},{0,1},{1,1},{0,0},{1,0},{0,1},{0.5,1.5},{1,1},{1,0}};  
% parametric: screw  
on rounded;  
w := for j := 1:200 collect {1/j*sin j, 1/j*cos j, j/200}$  
plot w;  
% parametric: globe  
dd := pi/15$  
w := for u := dd step dd until pi-dd collect  
    for v := 0 step dd until 2pi collect  
      {sin(u)*cos(v), sin(u)*sin(v), cos(u)}$  
plot w;  
% implicit: superposition of polynomials  
plot((x^2+y^2-9)*x*y = 0);

Piecewise-defined functions

A composed graph can be defined by a rule-based operator. In that case each rule must contain a clause which restricts the rule application to numeric arguments, e.g.

   operator my_step1;  
   let {my_step1(~x) => -1 when numberp x and x<-pi/2,  
        my_step1(~x) =>  1 when numberp x and x>pi/2,  
        my_step1(~x) => sin x  
            when numberp x and -pi/2<=x and x<=pi/2};  

Of course, such a rule may call a procedure:

   procedure my_step3(x);  
      if x<-1 then -1 else if x>1 then 1 else x;  
   operator my_step2;  
   let my_step2(~x) => my_step3(x) when numberp x;  

The direct use of a produre with a numeric if clause is impossible.

Plot options

The following plot options are supported in the plot command:

Additional options

The following additional GNUPLOT options are supported in the plot command:

16.27.3 Paper output

The following example works for a PostScript printer. If your printer uses a different communication, please find the correct setting for the terminal variable in the GNUPLOT documentation.

For a PostScript printer, add the options terminal=postscript and output=~filename~ to your plot command, e.g.

plot(sin x, x=(0 .. 10), terminal=postscript,;

16.27.4 Mesh generation for implicit curves

The basic mesh for finding an implicitly-given curve, the x,y plane is subdivided into an initial set of triangles. Those triangles which have an explicit zero point or which have two points with different signs are refined by subdivision. A further refinement is performed for triangles which do not have exactly two zero neighbours because such places may represent crossings, bifurcations, turning points or other difficulties. The initial subdivision and the refinements are controlled by the option points which is initially set to 20: the initial grid is refined unconditionally until approximately points * points equally-distributed points in the x,y plane have been generated.

The final mesh can be visualized in the picture by setting

    on show_grid;

16.27.5 Mesh generation for surfaces

By default the functions are computed at predefined mesh points: the ranges are divided by the number associated with the option points in both directions.

For two dimensions the given mesh is adaptively smoothed when the curves are too coarse, especially if singularities are present. On the other hand refinement can be rather time-consuming if used with complicated expressions. You can control it with the option refine. At singularities the graph is interrupted.

In three dimensions no refinement is possible as GNUPLOT supports surfaces only with a fixed regular grid. In the case of a singularity the near neighborhood is tested; if a point there allows a function evaluation, its clipped value is used instead, otherwise a zero is inserted.

When plotting surfaces in three dimensions you have the option of hidden line removal. Because of an error in Gnuplot 3.2 the axes cannot be labeled correctly when hidden3d is used ; therefore they aren’t labelled at all. Hidden line removal is not available with point lists.

16.27.6 GNUPLOT operation

The command plotreset; deletes the current GNUPLOT output window. The next call to plot will then open a new one.

If GNUPLOT is invoked directly by an output pipe (UNIX and Windows), an eventual error in the GNUPLOT data transmission might cause GNUPLOT to quit. As REDUCE is unable to detect the broken pipe, you have to reset the plot system by calling the command plotreset; explicitly. Afterwards new graphics output can be produced.

Under Windows 3.1 and Windows NT, GNUPLOT has a text and a graph window. If you don’t want to see the text window, iconify it and activate the option update wgnuplot.ini from the graph window system menu - then the present screen layout (including the graph window size) will be saved and the text windows will come up iconified in future. You can also select some more features there and so tailor the graphic output. Before you terminate REDUCE you should terminate the graphic window by calling plotreset;. If you terminate REDUCE without deleting the GNUPLOT windows, use the command button from the GNUPLOT text window - it offers an exit function.

16.27.7 Saving GNUPLOT command sequences

GNUPLOT If you want to use the internal GNUPLOT command sequence more than once (e.g. for producing a picture for a publication), you may set

on trplot, plotkeep;

trplot causes all GNUPLOT commands to be written additionally to the actual REDUCE output. Normally the data files are erased after calling GNUPLOT, however with plotkeep on the files are not erased.

16.27.8 Direct Call of GNUPLOT

GNUPLOT has a lot of facilities which are not accessed by the operators and parameters described above. Therefore genuine GNUPLOT commands can be sent by REDUCE. Please consult the GNUPLOT manual for the available commands and parameters. The general syntax for a GNUPLOT call inside REDUCE is

    gnuplot(<cmd>,<p_1>,<p_2> ...)

where cmd is a command name and p1,p2, are the parameters, inside REDUCE separated by commas. The parameters are evaluated by REDUCE and then transmitted to GNUPLOT in GNUPLOT syntax. Usually a drawing is built by a sequence of commands which are buffered by REDUCE or the operating system. For terminating and activating them use the REDUCE command plotshow. Example:

     gnuplot(plot, x*sin x);  

In this example the function expression is transferred literally to GNUPLOT, while REDUCE is responsible for computing the function values when plot is called. Note that GNUPLOT restrictions with respect to variable and function names have to be taken into account when using this type of operation. Important: String quotes are not transferred to the GNUPLOT executable; if the GNUPLOT syntax needs string quotes, you must add doubled stringquotes inside the argument string, e.g.

     gnuplot(plot, ~~~mydata~~~, ~using 2:1~);

16.27.9 Examples

The following are taken from a collection of sample plots (gnuplot.tst) and a set of tests for plotting special functions. The pictures are made using the qt GNUPLOT device and using the menu of the graphics window to export to PDF or PNG.

A simple plot for sin(1∕x):

plot(sin(1/x), x=(-1 .. 1), y=(-3 .. 3));


Some implicitly-defined curves:

plot(x^3 + y^3 - 3*x*y = {0,1,2,3}, x=(-2.5 .. 2), y=(-5 .. 5));


A test for hidden surfaces:

plot(cos sqrt(x^2 + y^2), x=(-3 .. 3), y=(-3 .. 3), hidden3d);


This may be slow on some machines because of a delicate evaluation context:

plot(sinh(x*y)/sinh(2*x*y), hidden3d);


on rounded;  
w:= {for j:=1 step 0.1 until 20 collect {1/j*sin j, 1/j*cos j, j},  
     for j:=1 step 0.1 until 20 collect  
       {(0.1+1/j)*sin j, (0.1+1/j)*cos j, j} }$  
plot w;


An example taken from: Cox, Little, O’Shea, Ideals, Varieties and Algorithms:

plot(point(3u+3u*v^2-u^3, 3v+3u^2*v-v^3, 3u^2-3v^2), hidden3d,  
     title=~Enneper Surface~);


The following examples use the specfn package to draw a collection of Chebyshev T polynomials and Bessel Y functions. The special function package has to be loaded explicitely to make the operator ChebyshevT and BesselY available.

load_package specfn;  
plot(chebyshevt(1,x), chebyshevt(2,x), chebyshevt(3,x),  
     chebyshevt(4,x), chebyshevt(5,x),  
     x=(-1 .. 1), title=~Chebyshev t Polynomials~);


plot(bessely(0,x), bessely(1,x), bessely(2,x), x=(0.1 .. 10),  
     y=(-1 .. 1), title=~Bessel functions of 2nd kind~);