The boxes library

The purpose of the boxes library is to align rectangles in 2D based on fairly general constraints. For instance, it can be used to make sub-plots in a figure line up nicely, with easy control over margins and dimensions.

The illustration above shows an example of this kind:

  • The figure is 6 units wide
  • It contains a square and a rectangle.
  • The rectangle has an aspect ratio of 2:1.
  • All spacings are 0.3 units.

How high should the figure be? — The answer is (6.0 − 0.3 × 3) / 3 + 0.3 × 2 = 2.3. Clearly, as figures become more complicated, solving these kinds of problems by hand quickly gets complicated. This is where this library comes in.

Below is code solving the above example:

from boxes import Context, constrain

ctx = Context()

# A box representing the figure. We only define its width.
fig = ctx.box(width=6)

# Create a square of any size, and a rectangle twice as wide
square = ctx.box(aspect=1)
rectangle = ctx.box(width=2 * square.width)

# Align the square and rectangle next to each other.
row = constrain.row(square, rectangle, spacing=0.3)

# Put them in the figure with 0.3 units of margins.
fig.pad(0.3).fix(row)

fig.solve()
print("The height of the figure is", fig.height)
The height of the figure is 2.3

This library supports any constraint which can ulitmately be expressed as a set of linear equations. See the boxes.constrain module for some examples.

Indices and tables