[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

8. Plotting


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

8.1 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}
      {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)
Function: plot2d ([name_1, ..., name_n], x_range, y_range)
Function: plot2d ([name_1, ..., name_n], x_range)
Function: plot2d (name, x_range, y_range)
Function: plot2d (name, 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.

When the function to be plotted is a function defined in Maxima by := or define, or in Lisp by DEFUN or DEFMFUN, the function can be specified by name. Functions defined in Lisp by DEFMSPEC, and simplifying functions, cannot be specified by name; that includes many built-in functions.

Examples:

Plotting an expression, and setting some commonly-used parameters.

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

Plotting functions by name.

 
(%i1) F(x) := x^2 $

(%i2) :lisp (defun |$g| (x) (m* x x x))

$g
(%i2) H(x) := if x < 0 then x^4 - 1 else 1 - x^5 $

(%i3) plot2d (F, [u, -1, 1])$

(%i4) plot2d ([F, G, H], [u, -1, 1])$

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:

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:

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:

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.

Examples:

 
plot2d (sin(x), [x, 0, 2*%pi], [gnuplot_term, ps], [gnuplot_out_file, "sin.eps"])$
 
plot2d ([gamma(x), 1/gamma(x)], [x, -4.5, 5], [y, -10, 10], [gnuplot_preamble, "set key bottom"])$
 
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]);
 
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"]);
 
plot3d (atan (-x^2 + y^3/4), [x, -4, 4], [y, -4, 4], [grid, 50, 50], [gnuplot_pm3d, true])$
 
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])$
 
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 (name, x_range, y_range, ..., options, ...)
Function: plot3d ([expr_1, expr_2, expr_3], x_rge, y_rge)
Function: plot3d ([name_1, name_2, name_3], x_range, y_range, ..., options, ...)

Displays a plot of one or three expressions as functions of two variables.

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

When the function to be plotted is a function defined in Maxima by := or define, or in Lisp by DEFUN or DEFMFUN, the function can be specified by name. Functions defined in Lisp by DEFMSPEC, and simplifying functions, cannot be specified by name; that includes many built-in functions.

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");

[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

This document was generated on March, 19 2006 using texi2html 1.76.