3.4.5 The box and circle Commands

Rectangular boxes and circles may be placed on multiplot canvases using the box and circle commands, as in:

box from 0*unit(mm),0*unit(mm) to 25*unit(mm),70*unit(mm)
circle at 0*unit(mm),0*unit(mm) radius 70*unit(mm)

In the former case, two corners of the rectangle are specified, meanwhile in the latter case the centre of the circle and its radius are specified. The box command may also be invoked by the synonym rectangle. Boxes may be rotated using an optional rotate modifier, which may be followed by a counter-clockwise rotational angle which may either have dimensions of angle, or is assumed to be in degrees if dimensionless. The rotation is performed about the centre of the rectangle:

box from 0,0 to 10,3 rotate 45

The positions and dimensions of boxes may also be specified by giving the position of one of the corners of the box, together with its width and height. The specified corner is assumed to be the bottom-left corner if both the specified width and height are positive; other corners may be specified if the supplied width and/or height are negative. If such boxes are rotated, the rotation is about the specified corner:

box at 0,0 width 10 height 3 rotate 45

The line type, line width, and colour of line with which the outlines of boxes and circles are drawn may be specified as in the arrow command, for example:

circle at 0,0 radius 5 with linetype 1 linewidth 2 colour red

The shapes may be filled by specifying a fillcolour:

circle at 0,0 radius 5 with lw 10 colour red fillcolour yellow

A simple no-entry sign.

In this example script, we use PyXPlot’s box and circle commands to produce a no-entry sign warning passers by that code monkeys can turn nasty when interrupted from their work.

set multiplot ; set nodisplay

w = 10 # Width of sign / cm

# Make no-entry sign
circle at 0,0 radius w with col null fillcol red
box from -(0.8*w),-(0.2*w) to (0.8*w),(0.2*w) $\backslash $
with col null fillcol white

# Put a warning beneath the sign
set fontsize 3
set texthalign centre ; set textvalign centre
text "$\backslash $bf Keep Out! Code Monkey at work!"

# Display sign
set display ; refresh

 

The resulting sign is shown below:

\includegraphics[width=5cm]{examples/eps/ex_noentry}

An image of the Mandelbrot Set.

The Mandelbrot set is a set of points in the complex plane, whose boundary forms a fractal. It is an iconic image of the power of chaos theory to produce complicated structure out of simple algorithms, which we implement in this example using PyXPlot’s loop constructs.

A point $c$ in the complex plane is defined to lie within the Mandelbrot set if the complex sequence of numbers

  \[  z_{n+1} = z_ n^2 + c,  \]    

subject to the starting condition $z_0=0$, remains bounded. In practice, the sequence is certain to diverge if $|z_ n|$ ever exceeds 2. It cannot generally be proven not to diverge at any particular point, but is considered to remain bounded in our algorithm if $|z_ n|$ has not exceeded 2 within a certain number StepsMax of steps. To increase the aesthetic appeal of the fractal, pixels outside of the fractal are assigned different colours depending upon how many iterations were required before the sequence moved outside the circle $|z_ n|=2$.

This script makes use of the rectangle command to paint each pixel of the fractal one by one, which is highly inefficient, but demonstrates PyXPlot’s versatility.

set multiplot
set nodisplay
set numerics complex
StepsMax=10
yn=0
for y=2 to -2 step -0.05
{
xn=0
for x=-2 to 2 step 0.05
{
zi = x+i*y; z=zi; iter=0;
while (abs(z)<2) and (iter<StepsMax) $\backslash $
{ ; z = z**2 + zi ; iter=iter+1 ; }
cd = iter/StepsMax
rectangle from xn,yn to xn+0.21,yn+0.21 with col null $\backslash $
fillc rgb(cd):(cd):(cd)
xn=xn+0.2
}
yn=yn+0.2
}

# Display output
set display
refresh

The resulting image is shown below:

\includegraphics[width=8cm]{examples/eps/ex_mandelbrot}