## STACK Documentation

Documentation home | Category index | Parent | Site map# Plots and graphics in STACK questions.

Plots and graphics can be placed into any of the CAStext fields.

The main way to create plots is using Maxima.

## plot()

In STACK, the `plot`

command has been defined to be a wrapper for Maxima's `plot2d`

command. The wrapper makes sure that an image file is given an appropriate name, file location, and returns a URL to the image. Not all of the features of `plot2d`

are available through `plot`

. In particular only a very few of Maxima's `plot_options`

are respected by `plot`

, mainly for security reasons. (`plot`

calls an external command `gnuplot`

which writes to the server filesystem.) Note that the `draw`

package is currently not supported.

Try, for example, the following in the question stem.

```
@plot(x^2,[x,-1,1])@
```

You can add a second variable to control the axes.

```
plot(x^2,[x,-1,1],[y,0,2])
```

However, Maxima will not always allow you to get the axes you want (this is a bug in Maxima). To get many plots in one window, we need to define a list of expressions.

```
plot([x^2,sin(x)],[x,-1,1])
```

This can be done with Maxima's `makelist`

command

```
(p(k):=x^k,pl:makelist(p(k),k,1,5),plot(pl,[x,-1,1]))
```

## Traditional axes

Note: this example only works with newer versions of Maxima (and not before 5.31.3).

A traditional plot with the axes in the middle can be generated by the following.

```
@plot([x^2/(1+x^2),2*x/(1+x^2)^2], [x, -2, 2], [y,-2.1,2.1], [box, false], [yx_ratio, 1], [axes, solid], [xtics, -2, 0.5, 2],[ytics, -2, 0.5, 2])@
```

## Labels

The `ylabel`

command rotates its argument through 90 degrees. If you want a horizontal label on the \(y\)-axis you will need to use the `label`

command instead.

```
@plot([x^2/(1+x^2),2*x/(1+x^2)^2], [x, -2, 2], [y,-2.1,2.1], [label,["y",-2.5,0.25]])@
```

## Grid

The grid is controlled by the maxima command `grid2d`

(newer versions of Maxima only). Compare the following.

```
@plot([x^2/(1+x^2),2*x/(1+x^2)^2], [x, -2, 2], [y,-2.1,2.1], grid2d)@
@plot([x^2/(1+x^2),2*x/(1+x^2)^2], [x, -2, 2], [y,-2.1,2.1])@
```

## Options

The following `plot`

options are currently supported by STACK. If you would like to expand the range of options available please contact the developers.

```
[xlabel, ylabel, label, legend, color, style, point_type, nticks, logx, logy, axes, box, plot_realpart, yx_ratio, xtics, ytics, ztic, grid2d],
```

Please see Maxima's documentation for more information on these options. Note that the `draw`

package is currently not supported.

Notes on some of the options.

- The default in Maxima is to include a legend consisting of a
`string`

representation of the plot. In STACK this is turned off by default. To switch it back on, use the command`[legend, true]`

. Any other value of the option`legend`

will respect the original command.

## Alternate text for an image (alt tag)

The default alternate text for an image (img alt tag) generated by a plot command such as

```
plot(x^2,[x,-2,2]);
```

is "STACK auto-generated plot of x^2 with parameters [[x,-2,2]]". If your question asks students to "give an algebraic expression which describes this curve" then you will need to set alternative text which does not include the answer.

To set a specific alt tag, pass an equation `[alt,"..."]`

as an argument to the plot function.

```
plot(x^2,[x,-2,2],[alt,"Hello world"]);
```

If you would like an expression as part of this then try

```
p:sin(x);
plot(p,[x,-2,2],[alt,sconcat("Here is ",string(p))]);
```

(Note, previous versions of STACK had the alt-text as an equation. This has been re-factored as a list, in line with other `plot2d`

options.)

## A catalogue of plots

The following CAStext gives representative examples of the plot2d features supported by STACK's plot command. Cut and paste it into the caschat script. Beware that these are likely to cause a timeout on the CAS if you try them all at once.

```
<h3>Basic plot</h3>
@plot(x^2,[x,-2,2])@
The following plot tests the option to explicitly test the alt-text.
@plot(x^3,[x,-3,3], [alt,"What is this function?"])@
<h3>Mutiple graphs, clips the \(y\) values</h3>
@plot([x^3,exp(x)],[x,-2,2],[y,-4,4])@
<h3>With and without a grid</h3>
@plot([x^2/(1+x^2),2*x/(1+x^2)^2], [x, -2, 2], [y,-2.1,2.1], grid2d)@
@plot([x^2/(1+x^2),2*x/(1+x^2)^2], [x, -2, 2], [y,-2.1,2.1])@
<h3>Discrete plots</h3>
Basic discrete plot.
@plot([discrete,[[0,0],[1,1],[0,2]]])@
Combination of discrete plots with normal plots.
@plot([x^2, [discrete,[ [0,0], [1,1], [0,2]]]],[x,-2,2])@
Using different point styles.
@plot([[discrete, [[10, .6], [20, .9], [30, 1.1],[40, 1.3], [50, 1.4]]],[discrete, [[11, .5], [15, .9], [25, 1.2],[40, 1.3], [50, 1.4]]]],[style, points],[point_type,circle,square],[color,black,green])@
<h3>Parametric plots</h3>
@plot([parametric, cos(t), sin(3*t), [t,0,2*%pi]], [nticks, 500])@
<h3>Setting non-trivial options: labels on the axes and legend</h3>
@plot([x^2/(1+x^2),diff(x^2/(1+x^2),x)],[x,-1,2],[legend,true])@
@plot(x*sin(1/x),[x,-1,2],[xlabel,"Independent variable"],[ylabel,"Dependent variable"],[legend,"This is a plot"])@
<h3>Log scale for y-axis, with red colour</h3>
@plot(exp(3*s),[s, -2, 2],[logy], [color,red])@
<h3>Turn off the box, grid and the axes</h3>
Default options
@plot([parametric, (exp(cos(t))-2*cos(4*t)-sin(t/12)^5)*sin(t), (exp(cos(t))-2*cos(4*t)-sin(t/12)^5)*cos(t), [t, -8*%pi, 8*%pi]], [nticks, 100])@
<tt>[axes, false]</tt>
@plot([parametric, (exp(cos(t))-2*cos(4*t)-sin(t/12)^5)*sin(t), (exp(cos(t))-2*cos(4*t)-sin(t/12)^5)*cos(t), [t, -8*%pi, 8*%pi]], [nticks, 100], [axes,false])@
<tt>[box, false]</tt>
@plot([parametric, (exp(cos(t))-2*cos(4*t)-sin(t/12)^5)*sin(t), (exp(cos(t))-2*cos(4*t)-sin(t/12)^5)*cos(t), [t, -8*%pi, 8*%pi]], [nticks, 100], [box,false])@
<h3>Putting the axes in the middle</h3>
@plot([x^2/(1+x^2),2*x/(1+x^2)^2], [x, -2, 2], [y,-2.1,2.1], [box, false], [yx_ratio, 1], [axes, solid], [xtics, -2, 0.5, 2],[ytics, -2, 0.5, 2])@
```

## implicit_plot()

In Maxima

```
load("implicit_plot");
implicit_plot(x^2+y^2=x^2*y^2+1,[x,-2,2],[y,-2,2]);
```

generates a plot of an implicit function.

Maxima's `implicit_plot()`

function does not respect the plot options, and we cannot place the resulting plot files in the correct places.
Hence, STACK does not currently support implicit plots.

## HTML

Note also that images can be included as HTML. It is easiest to place your image somewhere on the internet and include a URL in the body of your STACK question.

## Google charts

**NOT YET REINSTATED IN STACK 3.**

In particular, you can dynamically generate a URL for Google charts and in this way include randomly generated diagrams.

An example question is included as

```
test-venn.xml
```

This includes the code in the question variables to create random objects.

```
a : 30 + rand(20);
b : 40 + rand(50);
anb : 5 + rand(20);
aub : a+b-anb;
```

Then, in the question stem we have the HTML which uses this. Note the way values of variables are inserted here.

```
<img src="http://chart.apis.google.com/chart?cht=v&chs=200x100&chd=t:@a@,@b@,0,@anb@,0,0&chdl=A|B">
```

This should look like the following, with in this case \(a=33\), \(b=65\), \(a\cap b=18\).

## See also

Documentation home | Category index | Parent | Site map