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

# Plotting

## Definitions for Plotting

Option variable: in_netmath
  Default value: `false`

  When `in_netmath` is `true`, `plot3d` prints OpenMath output to the console if `plot_format` is `openmath`; otherwise `in_netmath` (even if `true`) has no effect.

  `in_netmath` has no effect on `plot2d`.

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 (parametric_expr)
Function: plot2d (discrete_expr)
Function: plot2d ([expr_1, ..., expr_n], x_range, y_range)
Function: plot2d ([expr_1, ..., expr_n], x_range)
Function: plot2d (expr, x_range, y_range)
Function: plot2d (expr, x_range)

  Displays a plot of one or more expressions as a function of one variable.

  In all cases, expr is an expression to be plotted on the vertical axis as a function of one variable. x_range, the range of the horizontal axis, is a list of the form `[variable, min, max]`, where variable is a variable which appears in expr. y_range, the range of the vertical axis, is a list of the form `[y, min, max]`.

  `plot2d (expr, x_range)` plots expr as a function of the variable named in x_range, over the range specified in x_range. If the vertical range is not otherwise specified by `set_plot_option`, it is chosen automatically. All options are assumed to have default values unless otherwise specified by `set_plot_option`.

  `plot2d (expr, x_range, y_range)` plots expr as a function of the variable named in x_range, over the range specified in x_range. The vertical range is set to y_range. All options are assumed to have default values unless otherwise specified by `set_plot_option`.

  `plot2d ([expr_1, ..., expr_n], x_range)` plots expr_1, ..., expr_n as a function of the variable named in x_range, over the range specified in x_range. If the vertical range is not otherwise specified by `set_plot_option`, it is chosen automatically. All options are assumed to have default values unless otherwise specified by `set_plot_option`.

  `plot2d ([expr_1, ..., expr_n], x_range, y_range)` plots expr_1, ..., expr_n as a function of the variable named in x_range, over the range specified in x_range. The vertical range is set to y_range. All options are assumed to have default values unless otherwise specified by `set_plot_option`.

  Examples:

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

  Anywhere there may be an ordinary expression, there may be a parametric expression: parametric_expr is a list of the form `[parametric, x_expr, y_expr, t_range, options]`. Here x_expr and y_expr are expressions of 1 variable var which is the first element of the range trange. The plot is of the path traced out by the pair `[x_expr, y_expr]` 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.

  Examples:

  - Plot a circle with a parametric plot.

        (%i1) plot2d ([parametric, cos(t), sin(t), [t, -%pi*2, %pi*2],
                [nticks, 80]])$

  - Plot a star: join eight points on the circumference of a circle.

        (%i2) plot2d ([parametric, cos(t), sin(t), [t, -%pi*2, %pi*2],
                [nticks, 8]])$

  - Plot a cubic polynomial with an ordinary plot and a circle with a parametric plot.

        (%i3) plot2d ([x^3 + 2, [parametric, cos(t), sin(t), [t, -5, 5],
                [nticks, 80]]], [x, -3, 3])$

  Discrete expressions may also be used instead or ordinary or parametric expressions: discrete_expr is a list of the form `[discrete, x_list, y_list]` or `[discrete, xy_list]`, where xy_list is a list of `[x,y]` pairs.

  Examples:

  - Create some lists.

        (%i1) xx:makelist(x,x,0,10)$
        (%i2) yy:makelist(exp(-x*1.0),x,0,10)$
        (%i3) xy:makelist([x,x*x],x,0,5)$

  - Plot with line segments.

        (%i4) plot2d([discrete,xx,yy])$

  - Plot with line segments, using a list of pairs.

        (%i5) plot2d([discrete,xy])$

  - Plot with points.

        (%i6) plot2d([discrete,xx,yy],[gnuplot_curve_styles,["with points"]])$

  - Plot the curve `cos(x)` using lines and (xx,yy) using points.

        plot2d([cos(x),[discrete,xx,yy]],[x,0,10],[gnuplot_curve_styles,["with lines","with points pointsize 3"]])$

  See also `plot_options`, which describes plotting options and has 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]);

  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 0.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.

System variable: plot_options
  Elements of this list state the default options for plotting. If an option is present in a `plot2d` or `plot3d` call, that value takes precedence over the default option. Otherwise, the value in `plot_options` is used. Default options are assigned by `set_plot_option`.

  Each element of `plot_options` is a list of two or more items. The first item is the name of an option, and the remainder comprises the value or values assigned to the option. In some cases the, the assigned value is a list, which may comprise several items.

  The plot options which are recognized by `plot2d` and `plot3d` are the following:

  - Option: `plot_format` determines which plotting package is used by `plot2d` and `plot3d`.
    - Default value: `gnuplot` Gnuplot is the default, and most advanced, plotting package. It requires an external gnuplot installation.
    - Value: `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.
    - Value: `openmath` Openmath is a Tcl/Tk GUI plotting program. It is included in the Maxima distribution.
    - Value: `ps` Generates simple PostScript files directly from Maxima. Much more sophisticated PostScript output can be generated from gnuplot, by leaving the option `plot_format` unspecified (to accept the default), and setting the option `gnuplot_term` to `ps`.
  - Option: `run_viewer` controls whether or not the appropriate viewer for the plot format should be run.
    - Default value: `true` Execute the viewer program.
    - Value: `false` Do not execute the viewer program.
  - `gnuplot_term` Sets the output terminal type for gnuplot.
    - Default value: `default` Gnuplot output is displayed in a separate graphical window.
    - Value: `dumb` Gnuplot output is displayed in the Maxima console by an "ASCII art" approximation to graphics.
    - Value: `ps` Gnuplot generates commands in the PostScript page description language. If the option `gnuplot_out_file` is set to filename, gnuplot writes the PostScript commands to filename. Otherwise, the commands are printed to the Maxima console.
  - Option: `gnuplot_out_file` Write gnuplot output to a file.
    - Default value: `false` No output file specified.
    - Value: filename Example: `[gnuplot_out_file, "myplot.ps"]` This example sends PostScript output to the file `myplot.ps` when used in conjunction with the PostScript gnuplot terminal.
  - Option: `x` The default horizontal range.

        [x, - 3, 3]

    Sets the horizontal range to [-3, 3].
  - Option: `y` The default vertical range.

        [y, - 3, 3]

    Sets the vertical range to [-3, 3].
  - Option: `t` The default range for the parameter in parametric plots.

        [t, 0, 10]

    Sets the parametric variable range to [0, 10].
  - Option: `nticks` Initial number of points used by the adaptive plotting routine.

        [nticks, 20]

    The default for `nticks` is 10.
  - Option: `adapt_depth` The maximum number of splittings used by the adaptive plotting routine.

        [adapt_depth, 5]

    The default for `adapt_depth` is 10.
  - Option: `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.
  - Option: `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)$

  - Option: `colour_z` is specific to the `ps` plot format.

        [colour_z, true]

    The default value for `colour_z` is `false`.
  - Option: `view_direction` Specific to the `ps` plot format.

        [view_direction, 1, 1, 1]

    The default `view_direction` is [1, 1, 1].

  There are several plot options specific to gnuplot. All of these options (except `gnuplot_pm3d`) are raw gnuplot commands, specified as strings. Refer to the gnuplot documentation for more details.

  - Option: `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`. Example:

        [gnuplot_pm3d, true]

  - Option: `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 the empty string `""`. Example:

        [gnuplot_preamble, "set log y"]

  - Option: `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`.) Example:

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

  - Option: `gnuplot_curve_styles` 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. Example:

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

  - Option: `gnuplot_default_term_command` The gnuplot command to set the terminal type for the default terminal. The default value is the empty string `""`, i.e., use gnuplot's default. Example:

        [gnuplot_default_term_command, "set term x11"]

  - Option: `gnuplot_dumb_term_command` 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. Example:

        [gnuplot_dumb_term_command, "set term dumb 132 50"]

  - Option: `gnuplot_ps_term_command` 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. Example:

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

  Examples:

  - Saves a plot of `sin(x)` to the file `sin.eps`.

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

  - 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 ([gamma(x), 1/gamma(x)], [x, -4.5, 5], [y, -10, 10], [gnuplot_preamble, "set key bottom"])$

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

      my_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)"$
      plot2d ([cos(x), sin(x), tan(x), cot(x)], [x, -2*%pi, 2*%pi],
          [y, -2, 2], [gnuplot_preamble, my_preamble]);

  - Uses a very complicated `gnuplot_preamble` to produce fancy x-axis labels, and produces PostScript output that takes advantage of the advanced text formatting available in gnuplot. (Note that the `gnuplot_preamble` string must be entered without any line breaks.)

      my_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)"$
      plot2d ([cos(x), sin(x), tan(x)], [x, -2*%pi, 2*%pi], [y, -2, 2],
          [gnuplot_preamble, my_preamble], [gnuplot_term, ps], [gnuplot_out_file, "trig.eps"]);

  - 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])$

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

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

  - A plot where the z-axis is represented by color only. (Note that the `gnuplot_preamble` string must be entered without any line breaks.)

      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])$

Function: plot3d (expr, x_range, y_range, ..., options, ...)
Function: plot3d ([expr_1, expr_2, expr_3], x_range, y_range, ..., options, ...)

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

  plots `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 plots a Moebius band, parametrized by the three 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.

  This example shows a plot of the 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` option 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 Klein bottle:

      expr_1: 5*cos(x)*(cos(x/2)*cos(y) + sin(x/2)*sin(2*y) + 3.0) - 10.0;
      expr_2: -5*sin(x)*(cos(x/2)*cos(y) + sin(x/2)*sin(2*y) + 3.0);
      expr_3: 5*(-sin(x/2)*cos(y) + cos(x/2)*sin(2*y));

      plot3d ([expr_1, expr_2, expr_3], [x, -%pi, %pi], [y, -%pi, %pi], ['grid, 40, 40]);

  or a torus

      expr_1: cos(y)*(10.0+6*cos(x));
      expr_2: sin(y)*(10.0+6*cos(x));
      expr_3: -6*sin(x);

      plot3d ([expr_1, expr_2, expr_3], [x, 0, 2*%pi], [y, 0, 2*%pi], ['grid, 40, 40]);

  We can output to gnuplot 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 over range.

  expr is an expression. range is a list of the form `[x, min, max]` in which x is a variable which appears in expr.

  See also `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)
  Assigns one of the global variables for plotting. option is specified as a list of two or more elements, in which the first element is one of the keywords on the `plot_options` list.

  `set_plot_option` evaluates its argument. `set_plot_option` returns `plot_options` (after modifying one of its elements).

  See also `plot_options`, `plot2d`, and `plot3d`.

  Examples:

  Modify the `grid` and `x` values. When a `plot_options` keyword has an assigned value, quote it to prevent evaluation.

      (%i1) set_plot_option ([grid, 30, 40]);
      (%o1) [[x, - 1.755559702014E+305, 1.755559702014E+305],
      [y, - 1.755559702014E+305, 1.755559702014E+305], [t, - 3, 3],
      [grid, 30, 40], [view_direction, 1, 1, 1], [colour_z, false],
      [transform_xy, false], [run_viewer, true],
      [plot_format, gnuplot], [gnuplot_term, default],
      [gnuplot_out_file, false], [nticks, 10], [adapt_depth, 10],
      [gnuplot_pm3d, false], [gnuplot_preamble, ],
      [gnuplot_curve_titles, [default]],
      [gnuplot_curve_styles, [with lines 3, with lines 1,
      with lines 2, with lines 5, with lines 4, with lines 6,
      with lines 7]], [gnuplot_default_term_command, ],
      [gnuplot_dumb_term_command, set term dumb 79 22],
      [gnuplot_ps_term_command, set size 1.5, 1.5;set term postscript #
      eps enhanced color solid 24]]
      (%i2) x: 42;
      (%o2)                          42
      (%i3) set_plot_option (['x, -100, 100]);
      (%o3) [[x, - 100.0, 100.0], [y, - 1.755559702014E+305,
      1.755559702014E+305], [t, - 3, 3], [grid, 30, 40],
      [view_direction, 1, 1, 1], [colour_z, false],
      [transform_xy, false], [run_viewer, true],
      [plot_format, gnuplot], [gnuplot_term, default],
      [gnuplot_out_file, false], [nticks, 10], [adapt_depth, 10],
      [gnuplot_pm3d, false], [gnuplot_preamble, ],
      [gnuplot_curve_titles, [default]],
      [gnuplot_curve_styles, [with lines 3, with lines 1,
      with lines 2, with lines 5, with lines 4, with lines 6,
      with lines 7]], [gnuplot_default_term_command, ],
      [gnuplot_dumb_term_command, set term dumb 79 22],
      [gnuplot_ps_term_command, set size 1.5, 1.5;set term postscript #
      eps enhanced color solid 24]]

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 (cmd)

  cmd is inserted in the PostScript file. Example:

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

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