J Labs

Plot Package

(1 of 22) Plot Overview

Plot is a plotting package for J providing business and scientific graphics.

To load, enter:


In [ ]:
load 'plot'

(2 of 22) Plot Overview (ctd)

This loads the package into locale plot, and defines two user functions, "pd" (Plot Driver) and "plot".

pd - low-level function that handles all calls to Plot, typically used for complex plots.

plot - cover function for pd that will handle most simple uses of Plot.

The examples given below also require the utilities in scripts numeric and trig. Load these as:


In [ ]:
load 'numeric trig'

(3 of 22) Verb "plot"

The form is:

```opt plot data```

The right argument is the data to be plotted. The optional left argument specifies various plot options.

In general, 2D plots require x and y values given as a 2-element boxed list, and 3D plots require x, y and z values as a 3-element boxed list.

However, if the right argument is open, it is treated as y values (2D plots) or z values (3D plots) . It should be a matrix of data. A vector is treated as a 1-row matrix. For 2D plots, each row of the matrix is treated as a separate data item.

(4 of 22) Verb "plot" (ctd)

The next section will plot a list of data. It is treated as y values, and the x values default to i.#y

(5 of 22) Verb "plot" (ctd)


In [ ]:
plot 1 2 3 5

(6 of 22) Verb "plot" (ctd)

The following also plots a list of data, the sin of y where y ranges from 0 to 10 in 100 steps. The data is treated as y values, and the x values again default to i.#y

This time, the default x values are inappropriate - they are shown as in the range 0 to 100, but are actually in the range 0 to 10:


In [ ]:
plot sin steps 0 10 100

(7 of 22) Verb "plot" (ctd)

The next example provides the correct x values as the first element of a boxed list of x and y values:


In [ ]:
x=: steps 0 10 100

In [ ]:
plot x;sin x

(8 of 22) Plot

The above example could have been entered more simply as:


In [ ]:
plot (];sin) steps 0 10 100

(9 of 22) Parametric Plot

The next example provides applies verbs to generate both the x and y values, giving a parametric plot:


In [ ]:
plot (sin;sin*cos) steps 0 10 100

(10 of 22) Parametric Plot (ctd)

It may be seen that all plots are parametric, but in the simpler cases, one of the verbs that generates the x or y values is the identity.

(11 of 22) Parametric Plot (ctd)

The next example plots the sin curve, swapping the identity and sin verbs to rotate the plot:


In [ ]:
plot (sin;]) steps 0 10 100

(12 of 22) Parametric Plot (ctd)

The above example could also have been entered as:


In [ ]:
plot |. (];sin) steps 0 10 100

(13 of 22) Plot Options

The left argument to plot specifies one or more options, delimited by semicolons. These are the options that may be given as arguments to pd, see below.

For example, show the x and y axes, and do not show any labels:


In [ ]:
'axes 1 1;labels 0' plot (sin;sin*cos) steps 0 10 100

(14 of 22) Plot Options (ctd)

The next example plots a matrix of sin values. Each row is treated as a separate data item:


In [ ]:
plot sin */~ steps 0 3 50

(15 of 22) Surface Plots

The "surface" option displays this as a surface plot:


In [ ]:
'surface' plot sin */~ steps 0 3 50

(16 of 22) Surface Plots (ctd)

In the above example, "surface" is short for "type surface".

If the first option is a type, the word "type" may be omitted.

3D graphics options include viewpoint, viewbox sizing, and the vertical direction. For example:


In [ ]:
Z=: sin */~ steps 0 3 50

In [ ]:
'surface;viewsize 1 1 0.2;viewpoint 1 0 0.7' plot Z

(17 of 22) Surface Plots (ctd)

Surface is the default type when x y and z values are provided in a 3-element boxed list.

The following example shows the standard sombrero:


In [ ]:
X=: Y=: steps _3 3 70

In [ ]:
Z=: (cos % 3&+) X (+/&:*:) Y

In [ ]:
'boxed 0' plot X;Y;Z

(18 of 22) Surface Plots (ctd)

This is the same sombrero, after use as a cushion:


In [ ]:
'boxed 0;viewsize 1 1 0.05' plot X;Y;Z

(19 of 22) Business Graphics

Plots for business graphics include various bar charts, pie charts, and hi-lo close.

For example, a stacked barchart:


In [ ]:
'sbar' plot >:?.>:i.3 5

(20 of 22) Business Graphics (ctd)

Plots for business graphics include various bar charts, pie charts, and hi-lo close.

Floating barchart, with title:


In [ ]:
'fbar;title My Plot' plot >:?>:i.3 5

(21 of 22) Verb "pd"

"pd" is a monadic function whose argument is either a list of commands or options given as a character string delimited by semicolons, or data given as numeric data.

For example:


In [ ]:
pd 'reset'         NB. reset plot

In [ ]:
pd 'type line'     NB. set line type

In [ ]:
pd *: i.10         NB. set plot data

In [ ]:
pd 'show'          NB. show it

(22 of 22) Plot Demo

For several examples using pd, load the plot demo from menu item Studio|Demos|plot.

In the demo, select menu item Options|View Definition to view and experiment with the definitions.

End of Lab


In [ ]: