.. _units:
There are several places in Toyplot where you will need to specify quantities with real-world units, including canvas dimensions, font sizes, and target dimensions for document-oriented backends such as :mod:toyplot.eps
and :mod:toyplot.pdf
. For example, when creating a canvas, whether explicitly or implicitly through the :ref:convenience-api
, you could specify its width and height in inches:
In [1]:
import numpy
x = numpy.linspace(0, 1)
y = x ** 2
In [2]:
import toyplot
toyplot.plot(x, y, width="3in", height="2in");
You can also specify the quantity and units separately:
In [3]:
toyplot.plot(x, y, width=(3, "in"), height=(2, "in"));
If you rendered either plot using the EPS or PDF backend, the resulting document size would be 3″ × 2″.
If you don't specify any units, the canvas assumes a default unit of CSS pixels:
In [4]:
toyplot.plot(x, y, width=600, height=400);
Note: You're probably used to treating pixels as dimensionless; however CSS Pixels are always 1/96th of an inch. Thus, the above example would produce a 6.25″ × 4.16″ PDF or EPS document.
If you rendered the same canvas using the PNG, MP4, or WebM backends, it would produce 600 × 400 pixel images / movies. Put another way, the backends that produce raster images always assume 96 DPI, unless overridden by the caller.
The units and abbreviations currently understood by Toyplot are as follows:
Functions that accept quantities with units as parameters will always accept them in either of two forms:
In addition, some functions will also accept a single numeric value, with a documented default unit of measure (such as the canvas width and height discussed above).
Further, some functions may accept quantities with "%" as the units. In this case, the quantity will be relative to some other documented value.
Toyplot style parameters always explicitly follow the CSS standard. As such, they support a subset of unit abbreviations including "cm", "in", "mm", "pc", "px", and "pt". Although CSS provides additional units for relative dimensioning, they assume that the caller understands their relationship to the underlying Document Object Model (DOM). Because Toyplot does not expose the DOM to callers and may change it at any time, these units are not supported.
In [ ]: