Go to the first, previous, next, last section, table of contents.


Plotting

Definitions for Plotting

Variable: IN_NETMATH [FALSE]
If not nil, then plot2d will output a representation of the plot which is suitable for openplot functions.

Function: OPENPLOT_CURVES list rest-options
Takes a list of curves such as
[[x1,y1,x2,y2,...],[u1,v1,u2,v2,...],..]
or 
 [[[x1,y1],[x2,y2],...],.. ]

and plots them. This is similar to xgraph_curves, but uses the open plot routines. Addtional symbol arguments may be given such as "{xrange -3 4}" The following plots two curves, using big points, labeling the first one jim and the second one jane.

openplot_curves([["{plotpoints 1} {pointsize 6} {label jim}
      {text {xaxislabel {joe is nice}}}"] ,
      [1,2,3,4,5,6,7,8],
      ["{label jane} {color pink } "], [3,1,4,2,5,7]]);

Some other special keywords are xfun, color, plotpoints, linecolors, pointsize, nolines, bargraph, labelposition, xaxislabel, and yaxislabel.

Function: PLOT2D (expr,range,...,options,..)
Function: PLOT2D ([expr1,expr2,..,exprn],xrange,...,options,..)
Function: PLOT2D (parametric_expr)
Function: PLOT2D ([..,expr,..,parametric_expr,..],xrange,...,options)

EXPR is an expression to be plotted on y axis as a function of 1 variable. RANGE is of the form [var,min,max] and expr is assumed to be an expression to be plotted against VAR. In the second form of the function a list of expressions may be given to plot against VAR. Truncation in the y direction will be performed, for the default y range. It may be specified as an option or using SET_PLOT_OPTION.

(%i1) plot2d(sin(x),[x,-5,5]);
(%i2) plot2d(sec(x),[x,-2,2],[y,-20,20],[nticks,200]);

Anywhere there may be an EXPR you may also use a parametric expression: PARAMETRIC_EXPR is a Maxima list of the form [parametric, xexpr, yexpr, trange, ..options] Here XEXPR and YEXPR are functions of 1 variable VAR which is the first element of the range TRANGE. The plot is of the path traced out by the pair [XEXPR, YEXPR] as VAR varies in TRANGE. In the following example, we plot a circle, then we do the plot with only a few points used, so that we get a star, and finally we plot this together with an ordinary function of X.

(%i1) plot2d([parametric,cos(t),sin(t),[t,-%pi*2,%pi*2],
        [nticks,80]]);
(%i2) plot2d([parametric,cos(t),sin(t),[t,-%pi*2,%pi*2],
        [nticks,8]]);
(%i3) plot2d([x^3+2,[parametric,cos(t),sin(t),[t,-5,5],
        [nticks,80]]],[x,-3,3]);

See PLOT_OPTIONS for more examples.

Function: xgraph_curves (list)
graphs the list of `point sets' given in list by using xgraph.

A point set may be of the form

[x0,y0,x1,y1,x2,y2,...] or
[[x0,y0],[x1,y1],....]

A point set may also contain symbols which give labels or other information.

 xgraph_curves([pt_set1,pt_set2,pt_set3]);

would graph the three point sets as three curves.

pt_set:append(["NoLines: True","LargePixels: true"],
        [x0,y0,x1,y1,...])

would make the point set [and subsequent ones], have no lines between points, and to use large pixels. See the man page on xgraph for more options to specify.

pt_set:append([concat("\"","x^2+y")],[x0,y0,x1,y1,...])

would make there be a "label" of "x^2+y" for this particular point set. The " at the beginning is what tells xgraph this is a label.

pt_set:append([concat("TitleText: Sample Data")],[x0,...])

would make the main title of the plot be "Sample Data" instead of "Maxima PLot".

To make a bar graph with bars which are .2 units wide, and to plot two possibly different such bar graphs:

xgraph_curves(
       [append(["BarGraph: true","NoLines: true","BarWidth: .2"],
               create_list([i-.2,i^2],i,1,3)),
        append(["BarGraph: true","NoLines: true","BarWidth: .2"],
           create_list([i+.2,.7*i^2],i,1,3))
]);

A temporary file `xgraph-out' is used.

Variable: PLOT_OPTIONS
Members of this list indicate defaults for plotting. They may be altered using SET_PLOT_OPTION

PLOT_FORMAT determines which plotting package will be used. The supported values for PLOT_FORMAT are:

[PLOT_FORMAT, GNUPLOT]

Gnuplot is the default, and most advanced, plotting package. It requires an external gnuplot installation.

[PLOT_FORMAT, MGNUPLOT]

Mgnuplot is a Tk-based wrapper around gnuplot. It is included in the Maxima distribution. Mgnuplot offers a rudimentary GUI for gnuplot, but has fewer overall features than the plain gnuplot interface. Mgnuplot requires an external gnuplot installation and TCL/Tk.

[PLOT_FORMAT, OPENMATH]

Openmath is a TCL/Tk GUI plotting program. It is included in the Maxima distribution.

[PLOT_FORMAT, PS]

The PS plot format generates simple postcript files directly from Maxima. Much more advanced postscript output is available using the gnuplot plot format and gnuplot_term set to ps.

RUN_VIEWER controls whether or not the appropriate viewer for the plot format should be run.

[RUN_VIEWER, FALSE]

The default value for RUN_VIEWER is TRUE.

GNUPLOT_TERM is specific to the gnuplot plot format. It sets the output terminal type for gnuplot

[GNUPLOT_TERM, DEFAULT]

The default gnuplot terminal is a separate graphical window.

[GNUPLOT_TERM, DUMB]

The dumb terminal produces an ASCII art approximation to graphics.

[GNUPLOT_TERM, PS]

The ps terminal produces postscript. Use it in conjunction with GNUPLOT_OUT_FILE in order to write postscript to a file instead of the screen.

GNUPLOT_OUT_FILE is specific to the gnuplot plot format. It sends gnuplot output to a file.

[GNUPLOT_OUT_FILE, "myplot.ps"]

This example sends postscript output to the file "myplot.ps" when used in conjunction with the postscript gnuplot terminal.

X and Y are the default horizontal and vertical ranges, respectively.

[X, - 3, 3]
[Y, - 3, 3]

Sets the horizontal and vertical ranges to [-3,3].

T is the default range for the parameter in parametric plots.

[T, 0, 10]

Sets the parametric variable range to [0, 10].

NTICKS and ADAPT_DEPTH control the initial number of points and the maximum number of splittings used by the adaptive plotting routine.

[NTICKS 20]
[ADAPT_DEPTH 5]

The default for both NTICKS and ADAPT_DEPTH is 10.

GRID sets the number of grid points to use in the x- and y-directions for three-dimensional plotting.

[GRID, 50, 50]

sets the grid to 50 by 50 points. The default grid is 30 by 30.

TRANSFORM_XY allows transformations to be applied to three-dimensional plots.

[TRANSFORM_XY, FALSE]

The default TRANSFORM_XY is FALSE. If it is not FALSE, it should be the output of

make_transform([x,y,z], f1(x,y,z),f2(x,y,z),f3(x,y,z))

The POLAR_XY transformation is built in. It gives the same transformation as

make_transform([r,th,z],r*cos(th),r*sin(th),z)

COLOUR_Z is specific to the PS plot format.

[COLOUR_Z, TRUE]

The default value for COLOUR_Z is FALSE.

VIEW_DIRECTION is specific to the PS plot format.

[VIEW_DIRECTION, 1, 1, 1]

The default VIEW_DIRECTION is [1,1,1].

GNUPLOT_PM3D, GNUPLOT_PREAMBLE, GNUPLOT_CURVE_TITLES, GNUPLOT_CURVE_STYLES, GNUPLOT_DEFAULT_TERM_COMMAND, GNUPLOT_DUMB_TERM_COMMAND and GNUPLOT_PS_TERM_COMMAND are advanced gnuplot options. All of these options (except GNUPLOT_PM3D) take raw gnuplot commands. Refer to the gnuplot documentation for more details.

[GNUPLOT_PM3D, TRUE]

GNUPLOT_PM3D controls the usage PM3D mode, which has advanced 3D features. PM3D is only available in gnuplot versions after 3.7. The default value for GNUPLOT_PM3D is FALSE.

[GNUPLOT_PREAMBLE, "set log y"]

GNUPLOT_PREAMBLE inserts gnuplot commands before the plot is drawn. Any valid gnuplot commands may be used. Multiple commands should be separated with a semi-colon. The example shown produces a log scale plot. The default value for GNUPLOT_PREAMBLE is "".

[GNUPLOT_CURVE_TITLES, ["my first function","my second function"]]

GNUPLOT_CURVE_TITLES controls the titles given in the plot key. The default value is DEFAULT, which automatically sets the title of each curve to the function plotted. If not DEFAULT, GNUPLOT_CURVE_TITLES should contain a list of strings. (To disable the plot key entirely, add "set nokey" to GNUPLOT_PREAMBLE.)

[GNUPLOT_CURVE_STYLES, ["with lines 7", "with lines 2"]]

GNUPLOT_CURVE_STYLES is a list of strings controlling the appearance of curves, i.e., color, width, dashing, etc., to be sent to the gnuplot plot command. The default value is ["with lines 3", "with lines 1", "with lines 2", "with lines 5", "with lines 4", "with lines 6", "with lines 7"], which cycles through different colors. See the gnuplot documentation for "plot" for more information.

[GNUPLOT_DEFAULT_TERM_COMMAND, "set term x11"]

GNUPLOT_DEFAULT_TERM_COMMAND is the gnuplot command to set the terminal type for the default terminal. The default value is "", i.e., use gnuplot's default.

[GNUPLOT_DUMB_TERM_COMMAND, "set term dumb 132 50"]

GNUPLOT_DUMB_TERM_COMMAND is the gnuplot command to set the terminal type for the dumb terminal. The default value is "set term dumb 79 22", which makes the text output 79 characters by 22 characters.

[GNUPLOT_PS_TERM_COMMAND, "set term postscript eps enhanced color solid 18"]

GNUPLOT_PS_TERM_COMMAND is the gnuplot command to set the terminal type for the postscript terminal. The default value is "set size 1.5, 1.5;set term postscript eps enhanced color solid 24", which sets the size to 1.5 times gnuplot's default, and the font size to 24, among other things. See the gnuplot documentation for "set term postscript" for more information.

Here are some examples using various plot options.

plot2d(sin(x),[x,0,2*%pi],[gnuplot_term,ps], [gnuplot_out_file,
"sin.eps"])

saves a plot of sin(x) to the file "sin.eps".

plot2d([gamma(x),1/gamma(x)],[x,-4.5,5],[y,-10,10],
[gnuplot_preamble,"set key bottom"])

uses the y option to chop off singularities and the gnuplot_preamble option to put the key at the bottom of the plot instead of the top.

plot2d([cos(x),sin(x),tan(x),cot(x)],[x,-2*%pi,2*%pi],[y,-2,2],
[gnuplot_preamble,
"set xzeroaxis; set xtics ('-2pi' -6.283, '-3pi/2' -4.712, '-pi' -3.1415, '-pi/2' -1.5708, '0' 0,'pi/2' 1.5708, 'pi' 3.1415,'3pi/2' 4.712, '2pi' 6.283)"])

uses a very complicated gnuplot_preamble to produce fancy x-axis labels. (Note that the above expression must be entered without any line breaks in the gnuplot_preamble string.)

plot2d([cos(x),sin(x),tan(x)],[x,-2*%pi,2*%pi],[y,-2,2],
[gnuplot_preamble,
"set xzeroaxis; set xtics ('-2{/Symbol p}' -6.283, '-3{/Symbol p}/2' -4.712, '-{/Symbol p}' -3.1415, '-{/Symbol p}/2' -1.5708, '0' 0,'{/Symbol p}/2' 1.5708, '{/Symbol p}' 3.1415,'3{/Symbol p}/2' 4.712, '2{/Symbol p}' 6.283)"],
[gnuplot_term,ps],[gnuplot_out_file,"trig.eps"])

is similar to the previous example, except that it produces postscript output that takes advantage of the advanced text formatting available in gnuplot. (Note that the above expression must be entered without any line breaks in the gnuplot_preamble string.)

plot3d(atan(-x^2+y^3/4),[x,-4,4],[y,-4,4], [grid,50,50],
[gnuplot_pm3d,true])

produces a three-dimensional plot using the gnuplot pm3d terminal.

plot3d(atan(-x^2+y^3/4),[x,-4,4], [y,-4,4], [grid,50,50],
[gnuplot_pm3d,true], [gnuplot_preamble,
"set pm3d at s;unset surface;set contour;set cntrparam levels 20;unset key"])

produces a three-dimensional plot without a mesh and with contours projected on the bottom plane.

plot3d(cos(-x^2+y^3/4),[x,-4,4], [y,-4,4], [gnuplot_preamble, 
"set view map; unset surface"], [gnuplot_pm3d,true], [grid,150,150]);

produces a plot where the z-axis is represented by color only. (Note that the above expression must be entered without any line breaks in the gnuplot_preamble string.)

Function: PLOT3D (expr,xrange,yrange,...,options,..)
Function: PLOT3D ([expr1,expr2,expr3],xrange,yrange,...,options,..)

plot3d(2^(-u^2+v^2),[u,-5,5],[v,-7,7]);

would plot z = 2^(-u^2+v^2) with u and v varying in [-5,5] and [-7,7] respectively, and with u on the x axis, and v on the y axis.

An example of the second pattern of arguments is

plot3d([cos(x)*(3+y*cos(x/2)),sin(x)*(3+y*cos(x/2)),y*sin(x/2)],
   [x,-%pi,%pi],[y,-1,1],['grid,50,15])

which will plot a moebius band, parametrized by the 3 expressions given as the first argument to plot3d. An additional optional argument [grid,50,15] gives the grid number of rectangles in the x direction and y direction.

/* Real part of z ^ 1/3 */
 plot3d(r^.33*cos(th/3),[r,0,1],[th,0,6*%pi],
     ['grid,12,80],['PLOT_FORMAT,ps],
     ['TRANSFORM_XY,POLAR_TO_XY],['VIEW_DIRECTION,1,1,1.4],
     ['COLOUR_Z,true])

Here the View_direction indicates the direction from which we take a projection. We actually do this from infinitely far away, but parallel to the line from view_direction to the origin. This is currently only used in 'ps' plot_format, since the other viewers allow interactive rotating of the object.

Another example is a moebius band:

plot3d([cos(x)*(3+y*cos(x/2)),
          sin(x)*(3+y*cos(x/2)),y*sin(x/2)],
           [x,-%pi,%pi],[y,-1,1],['grid,50,15]);

or a klein bottle:

plot3d([5*cos(x)*(cos(x/2)*cos(y)+sin(x/2)*sin(2*y)+3.0) - 10.0,
          -5*sin(x)*(cos(x/2)*cos(y)+sin(x/2)*sin(2*y)+3.0),
           5*(-sin(x/2)*cos(y)+cos(x/2)*sin(2*y))],
           [x,-%pi,%pi],[y,-%pi,%pi],['grid,40,40])

or a torus

plot3d([cos(y)*(10.0+6*cos(x)),
           sin(y)*(10.0+6*cos(x)),
           -6*sin(x)], [x,0,2*%pi],[y,0,2*%pi],
            ['grid,40,40])

We can output to gnplot too:

 plot3d(2^(x^2-y^2),[x,-1,1],[y,-2,2],[plot_format,gnuplot])

Sometimes you may need to define a function to plot the expression. All the arguments to plot3d are evaluated before being passed to plot3d, and so trying to make an expression which does just what you want may be difficult, and it is just easier to make a function.

M:MATRIX([1,2,3,4],[1,2,3,2],[1,2,3,4],[1,2,3,3])$
f(x,y):=float(M[?round(x),?round(y)]);
plot3d(f,[x,1,4],[y,1,4],['grid,4,4]);

See PLOT_OPTIONS for more examples.

Function: MAKE_TRANSFORM (vars, fx, fy, fz)
returns a function suitable for the transform function in plot3d. Use with the PLOT_OPTION TRANSFORM_XY.
make_transform([r,th,z],r*cos(th),r*sin(th),z)

is a transformation to polar coordinates.

Function: PLOT2D_PS (expr,range)
writes to pstream a sequence of postscript commands which plot EXPR for RANGE. EXPR should be an expression of 1 variable. RANGE should be of the form [variable,min,max] over which to plot expr. see CLOSEPS.

Function: CLOSEPS ()
This should usually becalled at the end of a sequence of plotting commands. It closes the current output stream PSTREAM, and sets it to nil. It also may be called at the start of a plot, to ensure pstream is closed if it was open. All commands which write to pstream, open it if necessary. CLOSEPS is separate from the other plotting commands, since we may want to plot 2 ranges or superimpose several plots, and so must keep the stream open.

Function: SET_PLOT_OPTION (option)
option is of the format of one of the elements of the PLOT_OPTIONS list. Thus
SET_PLOT_OPTION([grid,30,40])

would change the default grid used by plot3d. Note that if the symbol grid has a value, then you should quote it here:

SET_PLOT_OPTION(['grid,30,40])

so that the value will not be substituted.

Function: PSDRAW_CURVE (ptlist)

Draws a curve connecting the points in PTLIST. The latter may be of the form [x0,y0,x1,y1,...] or [[x0,y0],[x1,y1],...] The function JOIN is handy for taking a list of x's and a list of y's and splicing them together. PSDRAW_CURVE simply invokes the more primitive function PSCURVE. Here is the definition:

(defun $psdraw_curve (lis)
  (p "newpath")
  ($pscurve lis)
  (p "stroke"))

Function: PSCOM (com)

COM will be inserted in the poscript file eg

  pscom("4.5 72 mul 5.5 72 mul translate  14 14 scale");


Go to the first, previous, next, last section, table of contents.