[Help] Plotting - wxMaxima-developers/wxmaxima GitHub Wiki

Plotting (having fundamentally to do with graphics) is a place where a graphical user interface will have to provide some extensions to the original program.

Embedding a plot into the work sheet

Maxima normally instructs the external program gnuplot to open a separate window for every diagram it creates. Since many times it is convenient to embed graphs into the work sheet instead wxMaxima provides its own set of plot functions that don’t differ from the corresponding maxima functions save in their name.

They are all prefixed by a wx.

In Maxima In wxMaxima
draw wxdraw
histogram wxhistogram
plot2d wxplot2d
plot3d wxplot3d

Making embedded plots bigger or smaller

The wx Maxima configure dialog provides a way to change the default size plots are created with, which sets the starting value of wxplot_size. The plotting routines of wxMaxima respect this variable that specifies the size of a plot in pixels.

It can always be queried or used to set the size of the plots:

wxplot_size:[1200,800]$
wxdraw2d(
    explicit(
        sin(x),
        x,1,10
    )
)$

If the size of only one plot is to be changed Maxima provides a canonical way to change an attribute only for the current cell. In this usage the specification wxplot_size = [value1, value2] is appended to the wxdraw2d() command, and is not part of the wxdraw2d command.

wxdraw2d(
    explicit(
        sin(x),
        x,1,10
    )
),wxplot_size=[1600,800]$

Better quality plots

Gnuplot doesn’t seem to provide a portable way of determining whether it supports the high-quality bitmap output that the cairo library provides. On systems where gnuplot is compiled to use this library the pngcairo option from the configuration menu (that can be overridden by the variable wxplot_pngcairo) enables support for antialiasing and additional line styles. If wxplot_pngcairo is set without gnuplot supporting this the result will be error messages instead of graphics.

Opening embedded plots in interactive gnuplot windows

If a plot was generated using the wxdraw-type commands (wxplot2d and wxplot3d isn’t supported by this feature) and the file size of the underlying gnuplot project isn’t way too high wxMaxima offers a right-click menu that allows to open the plot in an interactive gnuplot window.

Opening gnuplot’s command console in plot windows

On MS Windows, if in Maxima’s variable gnuplot_command gnuplot is replaced by wgnuplot, gnuplot offers the possibility to open a console window, where gnuplot commands can be entered into. Unfortunately, enabling this feature causes gnuplot to “steal” the keyboard focus for a short time every time a plot is prepared.

Embedding animations into the woorkbook

3D diagrams tend to make it hard to read quantitative data. A viable alternative might be to assign the 3rd parameter to the mouse wheel. The with_slider_draw command is a version of wxdraw2d that does prepare multiple plots and allows to switch between them by moving the slider on top of the screen. wxMaxima allows to export this animation as an animated *.gif.

The first two arguments for with_slider_draw are the name of the variable that is stepped between the plots and a list of the values of these variable. The arguments that follow are the ordinary arguments for wxdraw2d.

with_slider_draw(
    f,[1,2,3,4,5,6,7,10],
    title=concat("f=",f,"Hz"),
    explicit(
        sin(2*%pi*f*x),
        x,0,1
    ),grid=true
);

The same functionality for 3D plots is accessible as with_slider_draw3d, which allows for rotating 3d plots.

wxanimate_autoplay:true;
wxanimate_framerate:20;
with_slider_draw3d(
    α,makelist(i,i,1,360,3),
    title=sconcat("α=",α),
    surface_hide=true,
    contour=both,
    view=[60,α],
    explicit(
        sin(x)*sin(y),
        x,-π,π,
        y,-π,π
    )
)$

If the general shape of the plot is what matters it might suffice to move the plot just a little bit in order to make its 3D nature available to the intuition.

wxanimate_autoplay:true;
wxanimate_framerate:20;
with_slider_draw3d(
    t,makelist(i,i,0,2*π,.05*π),
    title=sconcat("α=",α),
    surface_hide=true,
    contour=both,
    view=[60,30+5*sin(t)],
    explicit(
        sin(x)*y^2,
        x,-2*π,2*π,
        y,-2*π,2*π
    )
)$

For those more familiar with plot than with draw there is a second set of functions.

  • with_slider
  • wxanimate

Normally the animations are played back or exported with the frame rate chosen in the configuration of wxMaxima. To set the speed an individual animation is played back the variable wxanimate_framerate can be used.

wxanimate(a, 10,
    sin(a*x), [x,-5,5]), wxanimate_framerate=6$

The animation functions use Maxima’s makelist command and therefore shares the pitfall that the slider variable’s value is substituted into the expression only if the variable is directly visible in the expression. Therefore the following example will fail.

f:sin(a*x);
with_slider_draw(
    a,makelist(i/2,i,1,10),
    title=concat("a=",float(a)),
    grid=true,
    explicit(f,x,0,10)
)$

If Maxima is explicitly asked to substitute the slider’s value plotting works fine instead.

f:sin(a*x);
with_slider_draw(
    b,makelist(i/2,i,1,10),
    title=concat("a=",float(b)),
    grid=true,
    explicit(
        subst(a=b,f),
        x,0,10
    )
)$

Opening multiple plots in contemporaneous windows

While not being a provided by wxMaxima this feature of Maxima (on setups that support it) sometimes comes in handily. The following example comes from a post from Mario Rodriguez to the Maxima mailing list.

load(draw);

/* Parabola in window #1 */
draw2d(terminal=[wxt,1],explicit(x^2,x,-1,1));

/* Parabola in window #2 */
draw2d(terminal=[wxt,2],explicit(x^2,x,-1,1));

/* Paraboloid in window #3 */
draw3d(terminal=[wxt,3],explicit(x^2+y^2,x,-1,1,y,-1,1));

Plotting multiple plots in the same window is possible, too.

wxdraw(
    gr2d(
        key="sin (x)",grid=[2,2],
        explicit(sin(x),x,0,2*%pi)),
    gr2d(
    key="cos (x)",grid=[2,2],
    explicit(cos(x),x,0,2*%pi))
);

The "Plot using draw" side pane

The Plot using draw sidebar hides a simple code generator that allows to generate scenes that make use of some of the flexibility of the draw package maxima comes with.

2D

Generates the skeleton of a draw() command that draws a 2D scene. This scene later has to be filled with commands that generate the scene’s contents, for example by using the buttons in the rows below the 2D button.

One helpful feature of the 2D button is that it allows to setup the scene as an animation in which a variable (by default it is t) has a different value in each frame. Often a moving 2D plot allows easier interpretation than the same data in a non-moving 3D one.

3D

Generates the skeleton of a draw() command that draws a 3D scene. If neither a 2D or a 3D scene are set up all of the other buttons set up a 2D scene that contains the command the button generates.

Expression

Appends a standard plot of an expression like sin(x), x*sin(x) or x^2+2*x-4 to the draw() command the cursor currently is in. If there is no draw command a 2D scene with the plot is generated. Each scene can be filled with any number of plots.

Implicit plot

Tries to find all points an expression like y=sin(x), y*sin(x)=3 or x^2+y^2=4 is true at and plots the resulting curve in the draw() command the cursor currently is in. If there is no draw command a 2D scene with the plot is generated.

Parametric plot

Steps a variable from a lower limit to an upper limit and uses two expressions like t*sin(t) and t*cos(t) for generating the x, y (and in 3D plots also z) coordinates of a curve that is put into the current draw command.

Points

Draws many points that can optionally be joined. The coordinates of the points are taken from a list of lists, a 2D array or one list or array for each axis.

Diagram title

Draws a title on the upper end of the diagram,

Axis

Sets up the axis.

Contour (Only for 3D plots)

Adds contour lines similar to the ones one can find in a map of a mountain to the plot commands that follow in the current draw() command and/or to the ground plane of the diagram. Alternatively this wizard allows skipping drawing the curves entirely only showing the contour plot.

Plot name

Adds a legend entry showing the next plot’s name to the legend of the diagram. An empty name disables generating legend entries for the following plots.

Line colour

Sets the line colour for the following plots the current draw command contains.

Fill colour

Sets the fill colour for the following plots the current draw command contains.

Grid

Pops up a wizard that allows to set up grid lines.

Accuracy

Allows to select an adequate point in the speed vs. accuracy tradeoff that is part of any plot program.

Make wxMaxima output both image files and embedded plots at once

The worksheet embeds *.png files. wxMaxima allows the user to specify where they should be generated.

wxdraw2d(
    file_name="test",
    explicit(sin(x),x,1,10)
);

If a different format is to be used it is easier to generate the images and then to import them into the worksheet again.

load("draw");
pngdraw(name,[contents]):=
(
    draw(
        append(
            [
                terminal=pngcairo,
                dimensions=wxplot_size,
                file_name=name
            ],
            contents
        )
    ),
    show_image(printf(false,"~a.png",name))
);
pngdraw2d(name,[contents]):=
    pngdraw(name,gr2d(contents));

pngdraw2d("Test",
        explicit(sin(x),x,1,10)
);

Set the aspect ratio of a plot

Not directly using Maxima, there are gnuplot commands for it.

wxdraw2d(
    proportional_axis=xy,
    explicit(sin(x),x,1,10)
),wxplot_size=[1000,1000];

Problems

Symptom

Plotting only shows a closed empty envelope with an error message. This means that wxMaxima could not read the file Maxima that was supposed to instruct gnuplot to create.

Suspected diagnosis

  • The plotting command is part of a third-party package like implicit_plot but this package was not loaded by Maxima’s load() command before trying to plot.
  • Maxima tried to do something the currently installed version of gnuplot isn’t able to understand. In this case, a file ending in .gnuplot located in the directory, which Maxima’s variable maxima_userdir is pointing, contains the instructions from Maxima to gnuplot. Most of the time, this file’s contents therefore are helpful when debugging the problem.
  • Gnuplot was instructed to use the pngcairo library that provides antialiasing and additional line styles, but it was not compiled to support this possibility. Solution: Uncheck the Use the cairo terminal for plot checkbox in the configuration dialog and don’t set wxplot_pngcairo to true from Maxima.
  • Gnuplot didn’t output a valid *.png file.

Symptom

Plotting an animation results in error: undefined variable.

Suspected diagnosis

The value of the slider variable by default is only substituted into the expression that is to be plotted if it is visible there.

Possible cure

Using a subst command that substitutes the slider variable into the equation to plot resolves this problem. At the end of section Embedding animations into the workbook you can see an example.